Compare_IEnumerable_IQueryable1
 
AsEnumerable and AsQueryable cast or convert to IEnumerable or IQueryable, respectively

AsEnumerable (of IEnumerable) is frequently used to switch from any Asqueryable(IQueryable) implementation of LINQ to objects (L2O), mostly because the former does not support functions that L2O has.

IEnumerable

  • IEnumerable exists in System.Collections Namespace.
  • IEnumerable is read-only, can move forward only over a collection, has only one  it can’t move backward and between the items.
  • IEnumerable is best to query data from in-memory collections like List, Array etc.
  • While query data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data.
  • IEnumerable is suitable for LINQ to Object and LINQ to XML queries.
  • IEnumerable supports deferred execution.
  • IEnumerable doesn’t support lazy loading. Hence not suitable for paging like scenarios.
  • Has only GetEnumerator method, and several Extension methods .
 
  • AsQueryable : System.Linq Namespace
  • Provides a set of static  methods for querying data structures that implement IQueryable<T>, provides functionality to evaluate queries against a specific data source a known/fixed datasource.
  • IQueryable exists in System.Linq Namespace. IQueryable is IEnuerable + reference from query provider and query expression.
  • IQueryable can move forward only over a collection, it can’t move backward and between the items.
  • IQueryable is best to query data from out-memory (like remote database, service) collections.
  • While query data from database, IQueryable execute select query on server side with all filters.
  • IQueryable is suitable for LINQ to SQL queries.
  • IQueryable supports deferred execution.
  • IQueryable supports custom query using CreateQuery and Execute methods.
  • Extension methods supports by IQueryable takes expression objects means expression tree.
 
IQueryable IEnumerable
Collection<T>.AsQueryAble method, Converts an IEnumerable or generic IEnumerable<T> to an IQueryable or generic IQueryable<T>. It is the base interface for generic collection that can be enumerated with foreach control stratement.
IQueryable exists in System.Linq Namespace IEnumerable exists in System.Collections Namespace.
IQueryable supports custom query using CreateQuery and Execute methods. IEnumerable doesn’t supports custom query.
IQueryable can move forward only over a collection, it can’t move backward and between the items. IEnumerable can move forward only over a collection, it can’t move backward and between the items.
   
IQueryable is best to query data from out-memory (like remote database, service) collections. IEnumerable execute select query on server side, load data in-memory on client side and then filter data.
When to Use:
  1. Fetching records from remote datasource
When To Use :
  1. Iterate with foreach
  2. Read Only collection
  3. (MoveNext) forward only cursors
  4. thread safety not reuired
  5. do not need adding, or removing on colection.