LINQ Interview Question #120
How can repeated LINQ operations affect performance?
LINQ Performance, EF Core LINQ, Expressions & Advanced Queries Senior Advanced
Quick Interview Answer
Repeated enumeration, repeated sorting, repeated materialization, and repeated execution of expensive predicates can add unnecessary work. With IQueryable, repeated enumeration can also result in repeated database queries.
Detailed Explanation
Repeated enumeration, repeated sorting, repeated materialization, and repeated execution of expensive predicates can add unnecessary work. With IQueryable, repeated enumeration can also result in repeated database queries.
Cache or materialize results when reuse is intentional and appropriate, but avoid caching stale or excessively large data merely to eliminate enumeration.
Code Example
var active = products
.Where(p => p.IsActive)
.ToList();
var count = active.Count;
var firstTen = active.Take(10).ToList();