I recently ran into an issue with IEnumerable
v. IQueryable
. The algorithm being used first performed an IQueryable
query to obtain a set of results. These were then passed to a foreach
loop, with the items instantiated as an Entity Framework (EF) class. This EF class was then used in the from
clause of a Linq to Entity query, causing the result to be IEnumerable
.
I'm fairly new to EF and Linq for Entities, so it took a while to figure out what the bottleneck was. Using MiniProfiling, I found the query and then converted all of the individual operations to a single IQueryable
Linq for Entities query. The IEnumerable
took 15 seconds and the IQueryable
took 0.5 seconds to execute. There were three tables involved and, after reading this, I believe that the IEnumerable
query was actually forming a three table cross-product and filtering the results.
Try to use IQueryables as a rule-of-thumb and profile your work to make your changes measurable.