IEnumerable VS IQueryable

IEnumerable VS IQueryable

29 Mar 2024
Intermediate
75.6K 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)

Shailendra Chauhan, Founder and CEO of ScholarHat by DotNetTricks, is a renowned expert in System Design, Software Architecture, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development. His skill set extends into emerging fields like Data Science, Python, Azure AI/ML, and Generative AI, making him a well-rounded expert who bridges traditional development frameworks with cutting-edge advancements. Recognized as a Microsoft Most Valuable Professional (MVP) for an impressive 9 consecutive years (2016–2024), he has consistently demonstrated excellence in delivering impactful solutions and inspiring learners.

Shailendra’s unique, hands-on training programs and bestselling books have empowered thousands of professionals to excel in their careers and crack tough interviews. A visionary leader, he continues to revolutionize technology education with his innovative approach.
Accept cookies & close this