What is difference between IEnumerable and IQueryable?

There are following differences between ADO.NET and Entity Framework:

IEnumerable IQueryable
Exists in System.Collections Namespace Exists in System.Linq Namespace
Best to query data from in-memory collections like List, Array etc. Best to query data from out-memory (like remote database, service) collections.
While querying data from database, IEnumerable execute select query on server side, load data in-memory on client side and then filter data. For Example:
DataContext context = new DataContext();
IEnumerable list = context.Employees.Where(p => p.Name.StartsWith("S"));
list = list.Take(10);

Generated SQL having no Top Keyword:
SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0] WHERE [t0].[EmpName] LIKE @p0
While querying data from database, IQueryable execute select query on server side with all filters. For Example:
DataContext context = new DataContext();
IQueryable list = context.Employees.Where(p => p.Name.StartsWith("S"));
list = list.Take(10);

Generated SQL having Top Keyword:
SELECT TOP 10 [t0].[EmpID], [t0].[EmpName],[t0].[Salary] FROM [Employee] AS [t0] WHERE [t0].[EmpName] LIKE @p0
Suitable for LINQ to Object and LINQ to XML queries. Suitable for LINQ to SQL queries.
Doesn’t support lazy loading. Hence not suitable for paging like scenarios. Support lazy loading. Hence it is suitable for paging like scenarios.
Doesn’t supports custom query. Supports custom query using CreateQuery() and Execute() methods.

 

Tagged , . Bookmark the permalink.

Leave a Reply