What is the difference between returning IQueryable
vs. IEnumerable
, when should one be preferred over the other?
Yes, both will give you deferred execution.
The difference is that IQueryable
For the IEnumerable
In code:
IQueryable custs = ...;
// Later on...
var goldCustomers = custs.Where(c => c.IsGold);
That code will execute SQL to only select gold customers. The following code, on the other hand, will execute the original query in the database, then filtering out the non-gold customers in the memory:
IEnumerable custs = ...;
// Later on...
var goldCustomers = custs.Where(c => c.IsGold);
This is quite an important difference, and working on IQueryable