What is difference between IEnumerable and IQueryable?
Ans. 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 inmemory on client side and then filter data.
For Example:
DataContext context = new DataContext();
IEnumerable<Employee> list =
context.Employees.Where(p =>
p.Name.StartsWith("S"));
list = list.Take<Employee>(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<Employee> list =
context.Employees.Where(p =>
p.Name.StartsWith("S"));
list = list.Take<Employee>(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.
Ans. 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 inmemory on client side and then filter data.
For Example:
DataContext context = new DataContext();
IEnumerable<Employee> list =
context.Employees.Where(p =>
p.Name.StartsWith("S"));
list = list.Take<Employee>(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<Employee> list =
context.Employees.Where(p =>
p.Name.StartsWith("S"));
list = list.Take<Employee>(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.
Comments
Post a Comment