Skip to content

Deferred vs immediate execution

Context: LINQ queries are not executed when defined, but when iterated (deferred). Some operators force immediate execution.

int[] numbers = { 1, 2, 3 };
var query = numbers.Where(n => n > 1); // not executed yet
numbers[0] = 10; // change source
foreach (var n in query) // executed now, uses updated data
{
Console.WriteLine(n); // 10,2,3
}

Live data views: Deferred execution allows you to define a query that always reflects the latest data (e.g., filtering a live collection).

Example: In Blazor, you can bind a UI list to a deferred query that updates when the source changes.