Minimal API vs Controller :👈 👉:ASP.NET Interview Questions-1 to 10

IEnumerable vs IQueryable

IEnumerable vs IQueryable in .NET

Both IEnumerable<T> and IQueryable<T> are used to query collections in .NET, but they execute queries differently.

Quick Comparison

Feature IEnumerable<T> IQueryable<T>
Namespace System.Collections.Generic System.Linq
Execution In Memory At Data Source
Query Type LINQ to Objects LINQ to Entities / Databases
Performance Loads all records first Fetches only required records
Filtering Application Server Database Server
Suitable For In-memory collections Entity Framework queries
SQL Generation No Yes
Scalability Less scalable for large datasets Highly scalable

What is IEnumerable?

IEnumerable executes the query against data already loaded into memory.


List<Employee> employees = GetEmployees();
var result = employees
                .Where(e => e.Department == "IT")
                .ToList();

The filtering happens in the application memory after all data has been loaded.

Example


var employees = dbContext.Employees.ToList();
var result = employees.Where(x => x.Salary > 50000);

Execution Flow:


Database
   |
   | Fetch All Records
   v
Application Memory
   |
   | Filter Records
   v
Result

Problem: If the Employees table has 1 million records, all records are loaded into memory before filtering.

What is IQueryable?

IQueryable builds an expression tree and lets the database execute the query.


var result = dbContext.Employees
                      .Where(e => e.Department == "IT");

The query is translated into SQL and executed by the database server.

Generated SQL


SELECT *
FROM Employees
WHERE Department = 'IT'

Only matching records are returned.

Execution Flow


Application
   |
   | Build Expression Tree
   v
Database
   |
   | Filter Records
   v
Result

This approach is more efficient for large datasets.

Practical Example

IEnumerable


IEnumerable<Employee> employees =
    dbContext.Employees;
var result = employees
                .Where(x => x.Salary > 50000)
                .Take(10);

Actual Process:


SELECT *
FROM Employees

All records are fetched first.

Then:


Where()
Take(10)

execute in memory.

IQueryable


IQueryable<Employee> employees =
    dbContext.Employees;
var result = employees
                .Where(x => x.Salary > 50000)
                .Take(10);

Generated SQL:


SELECT TOP 10 *
FROM Employees
WHERE Salary > 50000

Only 10 filtered rows are returned from the database.

Deferred Execution

Both IEnumerable and IQueryable support deferred execution.


var query =
    dbContext.Employees
             .Where(x => x.IsActive);

No query runs yet.

Execution happens when:


query.ToList();
query.FirstOrDefault();
query.Count();

When IEnumerable is Appropriate

  • Working with in-memory collections
  • Small datasets
  • Data already loaded from the database
  • List<T>, Array, Dictionary collections

Example


List<string> names =
new List<string>
{
    "John",
    "David",
    "Sam"
};
var result =
names.Where(x => x.StartsWith("J"));

IEnumerable is perfectly suitable.

When IQueryable is Appropriate

  • Entity Framework
  • Large datasets
  • Server-side filtering
  • Server-side paging
  • Sorting in SQL Server

Example


var employees =
dbContext.Employees
         .Where(x => x.IsActive)
         .Skip(100)
         .Take(20);

Only required records are fetched from the database.

Performance Comparison

IEnumerable


var result =
dbContext.Employees
         .ToList()
         .Where(x => x.Salary > 50000);

Process:


Load All Rows
      |
      v
Filter In Memory
      |
      v
Return Result

IQueryable


var result =
dbContext.Employees
         .Where(x => x.Salary > 50000);

Process:


Generate SQL
      |
      v
Filter In Database
      |
      v
Return Result

For large datasets, IQueryable usually provides much better performance.

Common Mistake

Avoid:


var employees =
dbContext.Employees
         .ToList()
         .Where(x => x.Department == "IT");

This loads all employees into memory first.

Use:


var employees =
dbContext.Employees
         .Where(x => x.Department == "IT");

The filtering is performed by the database.

Best Practices

  • Use IQueryable while building Entity Framework queries.
  • Apply filtering, paging, and sorting before calling ToList().
  • Convert to IEnumerable only when data must be processed in memory.
  • Avoid calling ToList() too early.
  • Use server-side pagination with Skip() and Take().
  • Allow SQL Server to perform heavy filtering whenever possible.

Industry Standard Approach

Most enterprise applications use IQueryable in the Repository, Service, or Data Access layer until all filtering, sorting, searching, and pagination are completed.


Repository
      |
      v
IQueryable
      |
      v
Where()
OrderBy()
Skip()
Take()
      |
      v
ToList()
      |
      v
DTO Response

This minimizes database traffic and improves application performance.

Senior Architect Recommendation

  • Use IQueryable for database queries.
  • Use IEnumerable for in-memory processing.
  • Delay execution until all filtering and pagination are applied.
  • Avoid unnecessary ToList() calls.

Interview Answer

IEnumerable executes queries against data already loaded into memory, whereas IQueryable builds an expression tree and executes the query at the data source, such as SQL Server. IEnumerable is suitable for in-memory collections and smaller datasets. IQueryable is preferred for Entity Framework and large datasets because filtering, sorting, and pagination are performed by the database, resulting in better performance and lower memory consumption.

🔙
Minimal API vs Controller :👈 👉:ASP.NET Interview Questions-1 to 10