IEnumerable Vs IQueryable In LINQ

In this article I will demonstrate how to write code via IEnumerable and IQueryable and what the differences are between them.

IEnumerable and IQueryable are used for data manipulation in LINQ from the database and collections.

For getting the data from the database, I have created a table named "Employee" that has some data and looks like:

Then creating the Data Context class (.dbml class) in your project that converts the database table named "Employee" as a class.

Now I will tell you the functionality and some basic differences between IEnumerable and IQueryable using the object of the Data Context class..

IEnumerable Code

SQL statement after execution of above query

After the execution of line number 18, the SQL statement will look like the following until the end:

IQueryable Code

SQL statement after execution of the preceding query

After the execution of line number 22, the SQL statement will look like:

But after the execution of line number 23, SQL statement will add the Top for the filtering.

In both syntaxes I am accessing the data from the Employee table and then taking only 3 rows from that data. 

Differences

IEnumerable

  1. IEnumerable exists in the System.Collections namespace.


     
  2. IEnumerable is suitable for querying data from in-memory collections like List, Array and so on.
     
  3. While querying data from the database, IEnumerable executes "select query" on the server-side, loads data in-memory on the client-side and then filters the data.


     
  4. IEnumerable is beneficial for LINQ to Object and LINQ to XML queries.

IQueryable

  1. IQueryable exists in the System.Linq Namespace.


     
  2. IQueryable is suitable for querying data from out-memory (like remote database, service) collections.
     
  3. While querying data from a database, IQueryable executes a "select query" on server-side with all filters.

     
  4. IQueryable is beneficial for LINQ to SQL queries.


Similar Articles