LINQ Interview Question #112

What is AsNoTracking and why is it useful with LINQ queries?

LINQ Performance, EF Core LINQ, Expressions & Advanced Queries Senior Advanced

Quick Interview Answer

AsNoTracking tells EF Core not to track returned entities in the change tracker. For read-only queries, this can reduce tracking overhead and memory usage.

Detailed Explanation

AsNoTracking tells EF Core not to track returned entities in the change tracker. For read-only queries, this can reduce tracking overhead and memory usage.

It should be used when the returned entities do not need to be updated through the same DbContext. DTO projections often do not require entity tracking in the first place.

Code Example

var products = await db.Products
    .AsNoTracking()
    .Where(p => p.IsActive)
    .OrderBy(p => p.Name)
    .ToListAsync();