IEnumerable vs IQueryable

            1. What is the IEnumerable interface?

            2. What is the IQueryable interface?


public interface IEnumerable<T> : IEnumerable


   IEnumerator<T> GetEnumerator();


IEnumerable <(Of <( T >)>) is the interface that enables generic collection classes to be enumerated by using the foreach statement. Generic collection classes support IEnumerable <(Of <( T >)>) just as non-generic collection classes such as ArrayList support IEnumerable.


To be capable to work with data outside of the process we have to use IQueryable interface which provides functionality to evaluate queries against a specific data source wherein the type of the data is known.


public interface IQueryable : IEnumerable


  Type ElementType { get; }

  Expression Expression { get; }

  IQueryProvider Provider { get; }



It has the Expression property of the Expression type. Expression, this is the base type from which most variants of Expression<T> are derived. It is designed to hold the expression tree associated with an instance of IQueryable. It is a data structure equivalent to the executable code found in a query expression


The Provider property gets IQueryProvider interface


public interface IQueryProvider


  IQueryable<TElement> CreateQuery<TElement>(Expression expression);

  TResult Execute<TResult>(Expression expression);




IQueryProvider interface needs to:

1.    Convert expression trees to T-SQL (domain-specific language)

2.    Execute T-SQL

3.    Map the results of the query to a single object or a sequence of objects

The ElementType property gets A Type that represents the type of the element(s) that are returned when the expression tree associated with this object is executed.


IQueryable<Contact> top10Contacts = (from c in contacts

                        select c).Take(10);



Trace.WriteLine("IEnumerable.ElementType: " + top10Contacts.ElementType.FullName);

Trace.WriteLine("IEnumerable.Expression : " + top10Contacts.Expression.ToString());

Trace.WriteLine("IEnumerable.IQueryProvider  : " + top10Contacts.Provider.ToString());

Trace.WriteLine("IQueryProvider.CreateQuery  : " + top10Contacts.Provider.CreateQuery(top10Contacts.Expression).ToString());






IEnumerable.ElementType: LINQ2SQL.Contact

IEnumerable.Expression : Table(Contact).Select(c => c).Take(10)

IEnumerable.IQueryProvider  : SELECT TOP (10) [t0].[ContactID] AS [ContactId], [t0].[FirstName]

FROM [Person].[Contact] AS [t0]


IQueryProvider.CreateQuery  : SELECT TOP (10) [t0].[ContactID] AS [ContactId], [t0].[FirstName]

FROM [Person].[Contact] AS [t0]




Answer from stackoverflow:


First of all, IQueryable<T> extends the IEnumerable<T> interface, so anything you can do with a "plain" IEnumerable<T>, you can also do with an IQueryable<T>.

IEnumerable<T> just has a GetEnumerator() method that returns an Enumerator<T> for which you can call its MoveNext() method to iterate through a sequence of T.

What IQueryable<T> has that IEnumerable<T> doesn’t are two properties in particular—one that points to a query provider (e.g., a LINQ to SQL provider) and another one pointing to a query expression representing the IQueryable<T> object as a runtime-traversable expression that can be understood by the given query provider (for the most part, you can’t give a LINQ to SQL expression to a LINQ to Entities provider without an exception being thrown).

The expression can simply be a constant expression of the object itself or a more complex tree of a composed set of query operators and operands. The query provider’s IQueryProvider.Execute() or IQueryProvider.CreateQuery() methods are called with an Expression passed to it, and then either a query result or another IQueryable is returned, respectively.



See also:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s