Entity Framework Core Interview Question #129
How can projection improve EF Core performance?
Performance, Raw SQL, Stored Procedures & Advanced Queries Senior Advanced
Quick Interview Answer
Projection with Select retrieves only the fields needed by the application instead of materializing complete entities. This reduces transferred data, memory usage, and materialization work. DTO projection is especially useful for read-only pages and APIs and also separates persistence entities from presentation contracts.
Detailed Explanation
Projection with Select retrieves only the fields needed by the application instead of materializing complete entities. This reduces transferred data, memory usage, and materialization work. DTO projection is especially useful for read-only pages and APIs and also separates persistence entities from presentation contracts.
Code Example
var rows = await db.Orders
.AsNoTracking()
.Select(o => new OrderSummaryDto
{
Id = o.Id,
CustomerName = o.Customer.Name,
Total = o.Total
})
.ToListAsync();