Deferred and Immediate Execution

Understand how and when LINQ queries are evaluated at runtime by exploring deferred and immediate execution.

We'll cover the following...

The timing of LINQ query execution depends on how the query is evaluated. There are essentially two ways of executing a LINQ query:

  • Deferred execution: The LINQ query only executes when we try to address an item from a resulting collection.

  • Immediate execution: The LINQ query executes immediately after construction.

Let us explore these concepts in detail.

Deferred execution

There are three steps to creating and running a LINQ query:

  1. Creating or attaining a data source

  2. Creating a query

  3. Executing the query

Deferred execution means that our query is not executed the moment it is created. We define a data source and query using top-level statements.