LINQ Interview Question #23

What is immediate execution in LINQ?

LINQ Fundamentals, Syntax, IEnumerable & IQueryable Junior Beginner

Quick Interview Answer

Immediate execution occurs when an operator must produce a result now rather than return a deferred sequence. Materialization methods such as ToList and ToArray enumerate the source immediately, and scalar operators such as Count, First, Any, and Sum also execute to produce their result.

Detailed Explanation

Immediate execution occurs when an operator must produce a result now rather than return a deferred sequence. Materialization methods such as ToList and ToArray enumerate the source immediately, and scalar operators such as Count, First, Any, and Sum also execute to produce their result.

For database IQueryable queries, a terminal operation commonly causes the provider to execute the translated query against the database.

Code Example

var list = numbers
    .Where(n => n > 10)
    .ToList();

int count = numbers.Count(n => n > 10);
bool any = numbers.Any(n => n > 10);