C# Interview Question #124

What is materialization in LINQ and Entity Framework Core?

Advanced LINQ, Expressions & Collections Senior Advanced

Quick Interview Answer

Materialization is the process of executing a query and creating in-memory objects from its results.

Detailed Explanation

Materialization is the process of executing a query and creating in-memory objects from its results.

Operations such as ToList, ToArray, First, Single, Count, and similar terminal operators cause query execution. In EF Core, this normally means SQL is generated and sent to the database.

Before materialization, an IQueryable query can usually continue to be composed. Therefore, filtering, sorting, projection, and pagination should generally be applied before materialization so the database can perform the work efficiently.

Code Example

var query = context.Products
    .Where(p => p.IsActive)
    .Select(p => new ProductDto
    {
        Id = p.Id,
        Name = p.Name
    });

// SQL executes here
var products = await query.ToListAsync();