Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is the difference between IEnumerable and IQueryable in C#?
The IEnumerable and IQueryable interfaces are both used for querying collections in C#, but they differ significantly in how they execute queries and handle data. Understanding these differences is crucial for writing efficient database queries and managing memory usage.
Key Differences
| Feature | IEnumerable | IQueryable |
|---|---|---|
| Namespace | System.Collections | System.Linq |
| Query Execution | In-memory (client-side) | Server-side (database) |
| Lazy Loading | Not supported | Supported |
| Extension Methods | Use functional objects | Use expression trees |
| Best For | In-memory collections | Database queries |
Syntax
Following is the syntax for declaring IEnumerable −
IEnumerable<T> collection = source.Where(predicate);
Following is the syntax for declaring IQueryable −
IQueryable<T> query = source.Where(predicate);
Using IEnumerable for In-Memory Collections
When working with IEnumerable, all filtering happens on the client side after data is loaded into memory −
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
public static void Main() {
List<string> socialMedias = new List<string> {
"Twitter", "Facebook", "Instagram", "TikTok", "LinkedIn"
};
IEnumerable<string> result = socialMedias.Where(s => s.StartsWith("T"));
Console.WriteLine("IEnumerable results:");
foreach (string item in result) {
Console.WriteLine(item);
}
}
}
The output of the above code is −
IEnumerable results: Twitter TikTok
Using IQueryable for Database Queries
IQueryable builds expression trees that get translated to SQL, allowing the database to handle filtering efficiently −
using System;
using System.Linq;
class SocialMedia {
public int ID { get; set; }
public string Name { get; set; }
}
class MockDbContext {
public IQueryable<SocialMedia> SocialMedias { get; set; }
public MockDbContext() {
SocialMedias = new List<SocialMedia> {
new SocialMedia { ID = 1, Name = "Twitter" },
new SocialMedia { ID = 2, Name = "Facebook" },
new SocialMedia { ID = 3, Name = "TikTok" },
new SocialMedia { ID = 4, Name = "Instagram" }
}.AsQueryable();
}
}
class Program {
public static void Main() {
MockDbContext dc = new MockDbContext();
IQueryable<SocialMedia> query = dc.SocialMedias
.Where(p => p.Name.StartsWith("T"))
.Take(1);
Console.WriteLine("IQueryable results:");
foreach (SocialMedia item in query) {
Console.WriteLine($"ID: {item.ID}, Name: {item.Name}");
}
}
}
The output of the above code is −
IQueryable results: ID: 1, Name: Twitter
Query Execution Comparison
IEnumerable Database Query
// This loads ALL records first, then filters in memory
IEnumerable<SocialMedia> list = dc.SocialMedias.Where(p => p.Name.StartsWith("T"));
list = list.Take(1);
SQL generated for IEnumerable −
SELECT [t0].[ID], [t0].[Name] FROM [SocialMedia] AS [t0] WHERE [t0].[Name] LIKE @p0
IQueryable Database Query
// This applies all filters at the database level
IQueryable<SocialMedia> list = dc.SocialMedias.Where(p => p.Name.StartsWith("T"));
list = list.Take(1);
SQL generated for IQueryable −
SELECT TOP 1 [t0].[ID], [t0].[Name] FROM [SocialMedia] AS [t0] WHERE [t0].[Name] LIKE @p0
Performance Considerations
IQueryable is more efficient for database operations because it translates the entire query to SQL, reducing network traffic and memory usage. IEnumerable loads data into memory first, which can be inefficient for large datasets but is perfect for in-memory collections.
Conclusion
Use IQueryable when working with databases or external data sources to leverage server-side filtering and reduce memory consumption. Use IEnumerable for in-memory collections where all data is already loaded and you need simple iteration or client-side filtering.
