LINQ Interview Question #24

What is materialization in LINQ?

LINQ Fundamentals, Syntax, IEnumerable & IQueryable Junior Beginner

Quick Interview Answer

Materialization means executing a query and storing its results in a concrete in-memory representation, commonly with ToList, ToArray, ToDictionary, or similar conversion operators.

Detailed Explanation

Materialization means executing a query and storing its results in a concrete in-memory representation, commonly with ToList, ToArray, ToDictionary, or similar conversion operators.

Materialization creates a snapshot of the results at that point. With database queries, it also marks an important boundary: operations performed after materialization generally happen in application memory rather than being translated to SQL.

Code Example

var products = await db.Products
    .Where(p => p.IsActive)
    .Select(p => new ProductDto
    {
        Id = p.Id,
        Name = p.Name
    })
    .ToListAsync();