Why prefer using linq on IQueryable over linq on IEnumerable?

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

Does IQueryable use lazy calautation and optimization while IEnumerable doesn't?

Does optimization include query reordering?

+4
source share
2 answers

Linq on IEnumerable uses Linq for objects - Linq on IQueryable uses what the query provider has done for standard query operators.

For ORMs such as Linq to SQL and Entity Framework, that is, to translate your Linq query into the corresponding SQL query in the database - filtering, etc. in a database, it is much preferable to move all this data into memory, since it will be much better performance.

+9
source

No, IQueryable is usually supported by database-oriented providers and supports SQL translation.
Any optimizations will be related to the SQL engine.

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

+3
source

Source: https://habr.com/ru/post/1383519/


All Articles