LINQ Interview Question #84

What does ToList do to a LINQ query?

Deferred Execution, Immediate Execution & Query Behavior Mid-Level Intermediate

Quick Interview Answer

ToList immediately enumerates the source and stores the results in a new List<T>. It creates an in-memory snapshot of the query result at that moment.

Detailed Explanation

ToList immediately enumerates the source and stores the results in a new List<T>. It creates an in-memory snapshot of the query result at that moment.

For an EF Core IQueryable query, ToList or ToListAsync normally causes SQL execution and materializes returned rows into application objects.

Code Example

var activeProducts = await db.Products
    .Where(p => p.IsActive)
    .ToListAsync();

// activeProducts is now an in-memory List<Product>.