liveBook · Manning

Deferred execution

As explained in .NET in Action, Second Edition

Deferred execution is evaluation of a query only when the resulting sequence is enumerated or otherwise requested.

A LINQ query built with Select returns an IEnumerable; it is not executed until code requests an enumerator. This separates building a query from evaluating it. section 3.1.1

Listing 3.9 LINQ query translated to methods
public static Lazy<IEnumerable<string>> AllFonts =
  new (() =>
    typeof(FiggleFonts)                             #1
    .GetProperties(
      BindingFlags.Public | BindingFlags.Static)
    .Select(p => p.Name));                          #2

Compare the query-syntax form with its method-chain equivalent: both produce an enumerable whose query waits for enumeration. section 3.1.1

How .NET in Action, Second Edition covers it

How deep it goes
Explains that Select returns IEnumerable and execution waits for enumeration, while deferring fuller treatment to later chapters.
Examples
Uses reflected font properties to build a name query, then adds skip and take paging.
What you can do after
Understand when a LINQ query is evaluated relative to requesting an enumerator.
How it connects
Connects LINQ, IEnumerable, extension methods, and enumerators.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.