IEnumerable and IQueryable are interfaces that allows you to manipulate and query collections of data. IEnumerable is inherited by IQueryable, that means IQueryable add features to IEnumerable interface.
IEnumerable vs IQueryable
|LINQ to Object and LINQ to XML
|LINQ to SQL
|NO lazy loading
|out-memory (serverside) queries
|doesn’t support custom query
|supports custom query
Reference code from microsoft reference source
The following code shows the implementation of the IQueryable.Take() method.
The method creates a query on source provider to return top N elements.
The following code shows the implementation of the IEnumerable.Take() method.
The method simply uses a foreach to return the top N elements.
The following example shows the difference between IQueryable and IEnumerable:
The first statement uses the IQueryable interface, in fact it performs a query on data source and assigns the result to an IEnumerable variable.
The next operation is executed in-memory because they queriy use IEnumerable interface.
We can use SQL profiler to monitor the executed query:
We can perform the same operations using IQueryable interface, all queries will be performed on data source:
The SQL query will filter the result, and will use the SQL statement “TOP(100)”: