Monday, June 3, 2013

IEnumerable Vs IQueryable

IQueryable

The
IQueryable interface allows the objects to be queried based on the LINQ-to-SQL. Objects which implement the IQuerieable interface can be further queried using LINQ queries and these additional queries will be executed on the underlying data source.

The below line first queries the list of employees from the datasource, the resultant object empList is further queried based on the department name of the employee, both the filters are done in the underlying datasource and only the final list of employees belonging to the sales department are returned and stored in the memory, using LINQ-to-Sql. Hence more efficient based on
IEnumerable based objects.

IQueryable empList = from e in dbContext.Employees
var SalesEmployees = empList.Where(e => e.DepartmentName = ‘Sales’);


IEnumerable

The
IEnumerable interface also allows the objects to be queried but the difference is that the query in executed on the primary object set and not on the underlying data source.

In the below lines of code the first line will get the list of employee objects from the underlying data source and stores it in the memory, the second line further queries the list of objects in the memory and filters out the employees belonging to the sales department using LINQ-to-Objects. Hence less efficient based on
IQueryable based objects.

IEnumerable empList = from e in dbContext.Employees
var SalesEmployees = empList.Where(e => e.DepartmentName = ‘Sales’);

Search Flipkart Products:
Flipkart.com

No comments: