LINQ Interview Question #90

What is the difference between AsEnumerable and ToList?

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

Quick Interview Answer

AsEnumerable does not materialize the source; it changes the compile-time query interface used for subsequent operations. ToList immediately enumerates the source and stores the results in a new list.

Detailed Explanation

AsEnumerable does not materialize the source; it changes the compile-time query interface used for subsequent operations. ToList immediately enumerates the source and stores the results in a new list.

With IQueryable, AsEnumerable creates a client-side LINQ boundary without necessarily executing at that exact call, while ToList executes and materializes the query immediately.

Code Example

var clientPipeline = db.Products
    .Where(p => p.IsActive)
    .AsEnumerable();

var snapshot = db.Products
    .Where(p => p.IsActive)
    .ToList();