Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why to prefer using linq on IQueryable over linq on IEnumerable?

I know I can use linq syntax on collections that implement IQueryable or IEnumerable.

Does IQueryable use lazy calaulation and optimization, while IEnumerable not?

Does optimization include reordering of the queries?

like image 334
Elad Benda Avatar asked Dec 16 '22 06:12

Elad Benda


2 Answers

Linq on an IEnumerable is using Linq to Objects - Linq on an IQueryable is using whatever the query provider has implemented for the standard query operators.

For ORMs like Linq to SQL and Entity Framework i.e. that means translating your Linq query into the corresponding SQL query on the database - filtering etc. on the database is much preferred to moving all that data into memory as it will have much better performance.

like image 148
BrokenGlass Avatar answered Dec 18 '22 20:12

BrokenGlass


No, IQueryable is typically supported by database-oriented providers and supports translation to SQL.
Any optimizations would be due to the SQL engine.

Both IQueryable and IEnumerable have deferred execution.
interface IQueryable<T> : IEnumerable<T> ...

like image 29
Henk Holterman Avatar answered Dec 18 '22 19:12

Henk Holterman