LINQ Interview Question #77
Which LINQ operators commonly use deferred execution?
Deferred Execution, Immediate Execution & Query Behavior Mid-Level Intermediate
Quick Interview Answer
Operators that return another sequence commonly use deferred execution. Examples include Where, Select, SelectMany, Take, Skip, Distinct, and many ordering or grouping operations.
Detailed Explanation
Operators that return another sequence commonly use deferred execution. Examples include Where, Select, SelectMany, Take, Skip, Distinct, and many ordering or grouping operations.
Some deferred operators stream elements as needed, while others, such as ordering, must buffer data before producing results. Deferred execution therefore does not mean every operator has the same memory or performance behavior.
Code Example
var query = products
.Where(p => p.IsActive)
.Select(p => p.Name)
.Take(10);
// Executed when enumerated.
foreach (var name in query)
Console.WriteLine(name);