What is the difference between IEnumerable and IQueryable in C#?
Compare IEnumerable and IQueryable in C#: in-memory vs server-side filtering, Func vs expression trees, and how to avoid over-fetching database rows.
Expected Interview Answer
IEnumerable<T> executes queries in memory using delegates and pulls all data to the client before filtering, while IQueryable<T> builds an expression tree that a provider (like Entity Framework) translates into a remote query, such as SQL, so filtering happens at the data source.
IEnumerable represents forward-only, in-process iteration; LINQ methods on it take Func delegates and run locally, meaning a Where on a database-backed IEnumerable fetches every row first. IQueryable extends IEnumerable but its LINQ methods take Expression<Func> instead, so the provider inspects the expression tree and pushes filtering, sorting and paging to the server. Choosing IQueryable for out-of-process data avoids over-fetching, but once you cast to IEnumerable or call ToList, remaining operators run in memory.
- IQueryable pushes filtering to the database, reducing data transfer
- IEnumerable is ideal for in-memory collections
- IQueryable enables provider-specific query translation
- Both support deferred execution and composition
- Choosing correctly avoids over-fetching large tables
AI Mentor Explanation
IEnumerable is like calling every player in the country to the ground, then picking your eleven once they arrive — huge wasted travel. IQueryable is like sending the selection criteria to each regional board so only qualifying players are dispatched. The filtering happens at the source, so you receive just the eleven you need rather than shipping everyone to sort locally.
Step-by-Step Explanation
Step 1
Identify the data source
In-memory collection points to IEnumerable; a database or remote source suits IQueryable.
Step 2
Note the method arguments
IEnumerable's Where takes Func<T,bool>; IQueryable's takes Expression<Func<T,bool>>.
Step 3
See where filtering runs
IEnumerable filters in memory after fetching; IQueryable translates to SQL and filters at the server.
Step 4
Compose the query
Both defer execution, so chained operators build up before running.
Step 5
Watch the boundary
Casting to IEnumerable or calling ToList forces the rest to run locally in memory.
What Interviewer Expects
- Func delegate vs Expression tree distinction
- Understanding of where filtering executes (client vs server)
- Awareness of over-fetching risks with IEnumerable on DB data
- That IQueryable inherits from IEnumerable
- How casting changes execution location
Common Mistakes
- Using IEnumerable on database queries and pulling all rows
- Thinking IQueryable always runs in memory
- Not knowing IQueryable takes expression trees, not delegates
- Calling ToList too early and losing server-side filtering
- Assuming there is no performance difference between them
Best Answer (HR Friendly)
“IEnumerable works with data already loaded in memory and filters it there, while IQueryable can send your query to a database so the filtering happens at the source. For large database tables IQueryable is usually far more efficient because it avoids pulling every row into the application first.”
Code Example
// IQueryable: Where is translated to SQL WHERE, runs on the DB
IQueryable<User> q = db.Users;
var adults = q.Where(u => u.Age >= 18); // SELECT ... WHERE Age >= 18
// Casting to IEnumerable forces in-memory filtering
IEnumerable<User> all = db.Users; // fetches every row
var adultsInMemory = all.Where(u => u.Age >= 18); // filtered locally
foreach (var u in adults)
Console.WriteLine(u.Name);Follow-up Questions
- Why does IQueryable use Expression<Func> instead of Func?
- What happens when you call ToList on an IQueryable early?
- Can every LINQ operator be translated to SQL?
- How does deferred execution apply to IQueryable?
- When is IEnumerable the better choice?
MCQ Practice
1. Which interface builds an expression tree for a provider to translate?
IQueryable's operators take Expression<Func>, letting a provider translate the query to SQL or another form.
2. A Where on a database-backed IEnumerable will?
IEnumerable executes locally, so all rows are fetched before the delegate filter runs in memory.
3. IQueryable inherits from which interface?
IQueryable<T> extends IEnumerable<T>, so it can also be enumerated in memory when needed.
Flash Cards
IEnumerable execution location? — In memory, client-side, using Func delegates.
IQueryable execution location? — At the data source via a translated expression tree, e.g. SQL.
Argument type difference? — IEnumerable uses Func<T,bool>; IQueryable uses Expression<Func<T,bool>>.
Main risk of IEnumerable on DB data? — Over-fetching — pulling all rows before filtering.
What does casting to IEnumerable do? — Forces remaining operators to run in memory locally.