Entity Framework, ToList vs AsEnumerable vs AsQueryable


  • Execute the query immediately


  • lazy (execute the query later)
  • Parameter: Func<TSource, bool>
  • Load EVERY record into application memory, and then handle/filter them. (e.g. Where/Take/Skip, it will select * from table1, into the memory, then select the first X elements) (In this case, what it did: Linq-to-SQL + Linq-to-Object)


  • lazy (execute the query later)
  • Parameter: Expression<Func<TSource, bool>>
  • Convert Expression into T-SQL (with the specific provider), query remotely and load result to your application memory.
  • That’s why DbSet (in Entity Framework) also inherits IQueryable to get the efficient query.
  • Do not load every record, e.g. if Take(5), it will generate select top 5 * SQL in the background. This means this type is more friendly to SQL Database, and that is why this type usually has higher performance and is recommended when dealing with a database.
  • So AsQueryable() usually works much faster than AsEnumerable() as it generate T-SQL at first, which includes all your where conditions in your Linq.

source : https://stackoverflow.com/questions/17968469/whats-the-differences-between-tolist-asenumerable-asqueryable


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.