IEnumerable VS IQueryable

IEnumerable VS IQueryable

29 Mar 2024
Intermediate
73.2K Views
4 min read
Learn with an interactive course and practical hands-on labs

ASP.NET MVC with WebAPI Course

The difference between IEnumerable and IQueryable: An Overview

In this LINQ Tutorial, you will learn the difference between IEnumerable and IQueryable with some actual Programming Examples.In LINQ to query data from databases and collections, we use IEnumerable and IQueryable for data manipulation.

Basically, IQueryable inherits IEnumerable, Hence IQueryable has all the features of IEnumerable and except this, it has its own features. Both have their own methodology to query data and data manipulation. Let’s see both the features and learn how to use both the features to boost your LINQ Query performance.

 difference between IEnumerable and IQueryable

What is IEnumerable?

  1. IEnumerable exists in System.Collections Namespace.

  2. IEnumerable can move forward only over a collection, it can’t move backward and between the items.

  3. IEnumerable is best for querying data from in-memory collections like List, Array, etc.

  4. While querying data from a database, IEnumerable executes a select query on the server side, loads data in memory on the client side, and then filters data.

  5. IEnumerable is suitable for LINQ to Object and LINQ to XML queries.

  6. IEnumerable supports deferred execution.

  7. IEnumerable doesn’t support custom queries.

  8. IEnumerable doesn’t support lazy loading. Hence not suitable for paging-like scenarios.

  9. Extension methods supported by IEnumerable take functional objects.

IEnumerable Example:

 MyDataContext dc = new MyDataContext ();
IEnumerable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
list = list.Take<Employee>(10);

Generated SQL statements of the above query will be :

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

Explanation:

In the above IEnumerable Example We took the Employee list where we have to find the employee name starting with s, But you noticed that in this query "top 10" is missing since IEnumerable filters record on the client side. Now Let's see in IQueryable.

What is IQueryable?

  1. IQueryable exists in the System. Linq Namespace.

  2. IQueryable can move forward only over a collection, it can’t move backward and between the items.

  3. IQueryable is best for querying data from out-memory (like remote database, service) collections.

  4. While querying data from a database, IQueryable executes the select query on the server side with all filters.

  5. IQueryable is suitable for LINQ to SQL queries.

  6. IQueryable supports deferred execution.

  7. IQueryable supports custom queries using CreateQuery and Execute methods.

  8. IQueryable supports lazy loading. Hence it is suitable for paging-like scenarios.

  9. Extension methods supported by IQueryable take expression objects, which means an expression tree.

IQueryable Example:

MyDataContext dc = new MyDataContext ();
IQueryable<Employee> list = dc.Employees.Where(p => p.Name.StartsWith("S"));
list = list.Take<Employee>(10); 

Generated SQL statements of the above query will be :

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

Explanation:

In IQueryable Notice that in this query "top 10" exists since IQueryable executes a query in an SQL server with all filters.

Summary:

In this article, I explained the difference between IEnumerable and IQueryable. I hope after reading this article you will be able to boost your LINQ query performance. I would like to have feedback from my blog readers. Please post your feedback, questions, or comments about this article. Enjoy Coding..!

FAQs

IQueryable is suitable for querying data from out-memory collections. On the other hand IEnumerable is good for In-Memory Collection queries, While querying data from a database, IQueryable executes a "select query" on server side with all filters.

It is used for querying data from external data sources that may not be in memory

It is the collection of extension methods for classes that implement IEnumerable and IQueryable interfaces, therefore the LINQ query applies to any object that implements the IEnumerable or IQueryable interface.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 9th time in a row (2016-2024). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this