Note: this article is published on 06/05/2023.
In this series of articles, we discuss Lazy Loading and related. There is no Lazy Loading Category on this site; we put the articles in the LINQ category because the behavior of Lazy Loading is based on or related to LINQ.
- Lazy Loading (0): Overview
- Lazy Loading (1): Difference Between IEnumerable and IQueryable --- this article
- Lazy Loading (2): HTML
- Lazy Loading (3): JavaScript
- Lazy Loading (4): EntityFramework
- Lazy Loading (5): Angular --- not done
A - Introduction
This article will discuss the major differences between interfaces of INumerable and IQueryable, such as
- Execution Time Scale: deferred feature
- Execution Space Scale: Client Side or Server Side, in Memory or in Database
- Extension Method Supporting
- Lazy Loading supporting --- not discussed
There are a lot of articles to discuss the same topic; besides listing the major features of the topic, we will concentrate on showing the results by a piece of running code.
The content of this article:
- A - Introduction
- B - Basic Features for IEnumerable and IQueryable
- C - Features we discuss
- D - Test Environment
- E - Test by SQL Profiler:
- Time Scale: Deferred Feature
- Space Scale: Client Side vs. Server Side
- F - Test by Debugging
- G - Summary
B - Basic Features for IEnumerable and IQueryable
They are listed below [ref], where Bold are the differences:
IEnumerable
- IEnumerable exists in System.Collections Namespace.
- IEnumerable can move forward only over a collection; it can't move backward and between the items.
- IEnumerable is best for querying data from in-memory collections like List, Array, etc.
- While querying data from a database, IEnumerable executes a select query on the server side, loads data in-memory on a client-side, and then filters data.
- IEnumerable is suitable for LINQ to Object and LINQ to XML queries.
- IEnumerable supports deferred execution.
- IEnumerable doesn't support custom queries.
- IEnumerable doesn't support lazy loading. Hence not suitable for paging like scenarios.
- Extension methods supported by IEnumerable takes functional objects.
IQueryable
- IQueryable exists in System. Linq Namespace.
- IQueryable can move forward only over a collection; it can't move backward and between the items.
- IQueryable is best for querying data from out-memory (like remote Database, service) collections.
- While querying data from a database, IQueryable executes the select query on the server side with all filters.
- IQueryable is suitable for LINQ to SQL queries.
- IQueryable supports deferred execution.
- IQueryable supports custom queries using CreateQuery and Execute methods.
- IQueryable support lazy loading. Hence it is suitable for paging like scenarios.
- Extension methods supported by IQueryable take expression objects means expression tree.
Note:
lIQueryable is derived from IEnumerable; therefore, all features IEnumerable has, lIQueryable will have. On the other hand, some features IQueryable has, IEnumerable might not have.

The following covers the major features:

C - Features we Discuss
The following features that we will examine and show in the rest of the article:
- IEnumerable
-
IEnumerable is best for querying data from in-memory collections like List, Array, etc.
-
While querying data from a database, IEnumerable executes a select query on the server side, loads data in-memory on a client-side, and then filters data.
-
IEnumerable is suitable for LINQ to Object and LINQ to XML queries.
-
IEnumerable supports deferred execution.
-
Extension methods supported by IEnumerable takes functional objects.
-
- IQueryable
-
IQueryable is best for querying data from out-memory (like remote Database, service) collections.
-
While querying data from a database, IQueryable executes the select query on the server side with all filters.
-
IQueryable is suitable for LINQ to SQL queries.
-
IQueryable supports deferred execution.
-
Extension methods supported by IQueryable take expression objects means expression tree.
-
Actually, No. 1, 2, and 3 say the same thing:
The Space (Location), the filter is running:
IEnumerable works in memory, step by step. Meaning, for example,
var q = from a in b
where a > 5
select a;
It creates a list out “b” depending on “where” then it creates another list for “select”. This is the behavior of IEnumerable or LINQ to Object and LINQ to XML.

While IQueryable or LINQ to SQL generates T-SQL at the backend (database) to be able to get the exactly requested data for us.

In short:
- IEnumerable: Client side

- IQueryable: Server side

No. 4 talks about the Time Scale: when the Query is running:

















Join the conversation! Your thoughts help the community grow.