IEnumerable VS IQueryable

 

While query data from database, IQueryable execute select query on server side with all filters (e.g SQL select statement on database server). IEnumerable filters the data on client side. (in memory in application)

IEnumerable Example

  1. MyDataContext dc = new MyDataContext ();
  2. IEnumerable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith(“S”));
  3. list = list.Take<Employee>(10);

Generated SQL statements of above query will be :

  1. SELECT [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0]
  2. WHERE [t0].[EmpName] LIKE @p0

Notice that in this query “top 10” is missing since IEnumerable filters records on client side

IQueryable Example

  1. MyDataContext dc = new MyDataContext ();
  2. IQueryable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith(“S”));
  3. list = list.Take<Employee>(10);

Generated SQL statements of above query will be :

  1. SELECT TOP 10 [t0].[EmpID], [t0].[EmpName], [t0].[Salary] FROM [Employee] AS [t0]
  2. WHERE [t0].[EmpName] LIKE @p0

Notice that in this query “top 10” is exist since IQueryable executes query in SQL server with all filters.

 

Source: IEnumerable VS IQueryable

Leave a Reply