What is the difference between IQueryable
and IEnumerable
?
See also What\'s the difference between IQueryable and I
First of all, IQueryableIEnumerable
interface, so anything you can do with a "plain" IEnumerable
, you can also do with an IQueryable
.
IEnumerable
just has a GetEnumerator()
method that returns an Enumerator
for which you can call its MoveNext()
method to iterate through a sequence of T.
What IQueryable
has that IEnumerable
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
object as a runtime-traversable abstract syntax tree 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.