Introduction
When working with LINQ in C#, developers often see two important interfaces:
Both are used to query data collections, but they work differently, especially when working with databases.
Understanding the difference helps developers write faster and more efficient applications.
In this article, we will understand the difference between IEnumerable and IQueryable with simple examples.
What is IEnumerable?
IEnumerable is an interface used for iterating over in-memory collections such as:
It executes the query after retrieving data from the database.
Simple Example
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
List<int> numbers = new List<int> { 1,2,3,4,5,6,7,8 };
IEnumerable<int> result = numbers.Where(n => n > 4);
foreach (var item in result)
{
Console.WriteLine(item);
}
}
}
Output
5
6
7
8
Important Point
Here the data is already loaded into memory, and then LINQ filters it.
So the filtering happens in the application memory.
What is IQueryable?
IQueryable is used to query data from external data sources, such as databases.
Instead of loading all data first, it builds a query and sends it to the database server.
The database performs the filtering.
Example with Database Concept
using System;
using System.Linq;
class Program
{
static void Main()
{
IQueryable<int> data = new List<int> {1,2,3,4,5,6}.AsQueryable();
var result = data.Where(x => x > 3);
foreach (var item in result)
{
Console.WriteLine(item);
}
}
}
In real projects, IQueryable is commonly used with Entity Framework.
Example
var students = db.Students.Where(s => s.Age > 18);
Here the query is converted into SQL like:
SELECT * FROM Students WHERE Age > 18
The filtering happens inside the database, not in memory.
Key Difference Between IEnumerable and IQueryable
| Feature | IEnumerable | IQueryable |
|---|
| Namespace | System.Collections | System.Linq |
| Execution | In-memory | Database/server side |
| Performance | Slower for large data | Faster for large data |
| Query Processing | Done in application | Done in database |
| Best For | Small collections | Large database queries |
Practical Example (Important)
Assume a table contains 100000 records.
Using IEnumerable
var students = db.Students.ToList().Where(s => s.Age > 18);
Process:
Load all 100000 records
Then filter in memory
This is slow and memory heavy.
Using IQueryable
var students = db.Students.Where(s => s.Age > 18);
Process:
This is much faster.
When to Use IEnumerable
Use IEnumerable when:
Example
List<string> names = new List<string>{"Rahul","Amit","John"};
IEnumerable<string> result = names.Where(n => n.StartsWith("A"));
When to Use IQueryable
Use IQueryable when:
Working with databases
Using Entity Framework
Handling large datasets
Example
var products = db.Products.Where(p => p.Price > 1000);
Common Mistake Developers Make
Many beginners write:
db.Products.ToList().Where(p => p.Price > 1000);
This loads all data first.
Better approach:
db.Products.Where(p => p.Price > 1000).ToList();
Now filtering happens in the database.
Conclusion
Both IEnumerable and IQueryable are important in LINQ, but they are used in different scenarios.
For large datasets and database operations, IQueryable provides better performance and efficiency.