LINQ Interview Question #149
What common LINQ mistakes should a senior developer watch for?
Advanced LINQ, Practical Problems & Senior Interview Scenarios Senior Advanced
Quick Interview Answer
Common mistakes include premature ToList calls, repeated enumeration, loading complete entities when a projection is sufficient, N+1 query patterns, missing deterministic ordering for pagination, unbounded queries, and accidental client-side processing.
Detailed Explanation
Common mistakes include premature ToList calls, repeated enumeration, loading complete entities when a projection is sufficient, N+1 query patterns, missing deterministic ordering for pagination, unbounded queries, and accidental client-side processing.
Other issues include incorrect First versus Single semantics, expensive Count checks where Any is enough, assuming every .NET method can be translated, and using concise LINQ without considering the SQL or algorithmic work it causes.
Code Example
var query = db.Products
.AsNoTracking()
.Where(p => p.IsActive)
.OrderBy(p => p.Id)
.Select(p => new ProductDto
{
Id = p.Id,
Name = p.Name
});
var page = await query
.Take(100)
.ToListAsync();