IEnumerable Vs IQueryable

Differences between them based on their properties :

IEnumerable

  1. NamespaceSystem.Collections Namespace
  2. Derives from: No base interface
  3. Deferred Execution : Supported
  4. Lazy Loading : Not Supported
  5. How does it work : While querying data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data. Hence does more work and becomes slow.
  6. Suitable for : LINQ to Object and LINQ to XML queries.
  7. Custom Query : Doesn’t supports.
  8. Extension method parameter : Extension methods supported in IEnumerable takes functional objects.
  9. When to use : When querying data from in-memory collections like List, Array etc.
  10. Best Uses : In-memory traversal

IQueryable

  1. Namespace: System.Linq Namespace
  2. Derives from: Derives from IEnumerable
  3. Deferred Execution : Supported
  4. Lazy Loading : Supported
  5. How does it work : While querying data from database, IQueryable execute select query on server side with all filters. Hence does less work and becomes fast.
  6. Suitable for : LINQ to SQL queries.
  7. Custom Query : Supports using CreateQuery and Execute methods.
  8. Extension method parameter : Extension methods supported in IEnumerable takes expression objects i.e. expression tree.
  9. When to use : When querying data from out-memory (like remote database, service) collections.
  10. Best Uses : Paging

Author:

Since March 2011, have 8+ years of professional experience on software development, currently working as Senior Software Engineer at s3 Innovate Pte Ltd.

Leave a Reply