Deferred execution

A query is a plan

A LINQ query is not a result and not a copy: it is an object that holds a reference to the source and a list of the steps to run over it. Nothing reads the source when the query is declared. The first walk runs every step for the items that walk reaches, and a second walk runs them again.

That is called deferred execution, and the first consequence is that a source changed after the query was written changes the answer:

var numbers = new List<int> { 1, 2, 3 }; var query = numbers.Where(n => n > 1); numbers.Add(4); numbers.Add(0); Console.WriteLine(string.Join(",", query)); Console.WriteLine($"count: {query.Count()}");

dotnet run

2,3,4
count: 3

The query was written over three items and printed three: 2, 3 and the 4 added afterwards. The 0 was added too and the filter rejected it, which is the other half of the proof: the query re-read the list, and the filter ran at that moment.

Every walk runs it again

Put a print inside the lambda passed to Select and the timing becomes visible. The program below prints one line per item per walk, so the two walks are easy to count:

var names = new[] { "a", "bb", "ccc" }; var query = names.Select(n => Explain(n)); Console.WriteLine("before the loop"); foreach (var name in query) { Console.WriteLine(name); } Console.WriteLine("second walk"); Console.WriteLine(query.Count()); static string Explain(string value) { Console.WriteLine($" reading {value}"); return value.ToUpperInvariant(); }

dotnet run

before the loop
  reading a
A
  reading bb
BB
  reading ccc
CCC
second walk
  reading a
  reading bb
  reading ccc
3

before the loop printed before any read, which is the deferral. The first walk interleaved a read with a print, because Select produces one item at a time and foreach consumes it before asking for the next: the two sequences run in lock step. The second walk ran the lambda three more times for Count(), which only needed the number.

This is the reason a query must not carry a side effect that matters. Select(n => WriteToDatabase(n)) writes on every walk, and a query walked four times writes four times. Work that must happen once belongs before the query, or the result belongs in a list.

ToList freezes the result

ToList, ToArray, ToDictionary, ToHashSet and the aggregate methods all run the query on the spot and return data. After that call there is no query left to re-run:

var source = new List<int> { 1, 2, 3 }; var lazy = source.Where(n => n > 0); var frozen = source.Where(n => n > 0).ToList(); source.Add(4); Console.WriteLine($"lazy {lazy.Count()} frozen {frozen.Count} type {frozen.GetType().Name}");

dotnet run

lazy 4 frozen 3 type List`1

Both expressions look alike and one is a plan over three items that now sees four, while the other is a List<int> of three made at the moment of the call. The backtick in List`1 is how the runtime names a generic type.

ToList is also the fix for the side effect above: a list walked twice runs nothing twice. It costs the memory of the copy and the time of one full walk, so it belongs at the end of a pipeline, not after every step.

Rules that keep it predictable

Exercises

  1. Add a print inside the Where predicate of the first program and count how many times it runs for the two walks.
  2. Put a .ToList() before the foreach of the second program and count the reads.
  3. Walk the third program twice, once with ToList and once without, and explain the difference in the two counts.
  4. Write a method that takes IEnumerable<int>, counts the items and then sums them. How often does the caller see its predicate run?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. When does a LINQ query read its source?

    Answer

    When the query is enumerated

    The query is a plan; the source is read by the walk, and every walk reads it again.

  2. What does this program print? var list = new List<int> { 1, 2 }; var q = list.Where(x => x > 0); list.Add(3); Console.WriteLine(string.Join(",", q));

    Answer

    1,2,3

    The query runs at the walk, which happens after the third item was added.

  3. What does ToList do to a query?

    Answer

    Runs it now and keeps the items in a list

    ToList is an eager operator: it walks the query immediately and returns a List<T>.

  4. How many times does the lambda of Select run in the second program of this lesson?

    Answer

    Six: three items, two walks

    The foreach walks once and Count() walks again, so three items are projected twice.

  5. Why is a side effect inside a Select lambda a bug waiting to happen?

    Answer

    The lambda runs once per item per walk

    Deferred execution repeats the work on every enumeration, so the side effect repeats too.

  6. What does this program print? var a = new[] { 1, 2, 3 }.Where(x => x > 1); Console.WriteLine(string.Join(",", a)); Console.WriteLine(string.Join(",", a));

    Answer

    2,3 and then 2,3

    Each walk runs the query again on the same array, so both lines show the same items.

  7. Which call turns a query into data right away?

    Answer

    ToArray

    ToArray, like ToList and the aggregate methods, enumerates the query at the call.

  8. A method takes IEnumerable<int> and walks it twice. What should it do?

    Answer

    Walk it once into a list and use the list

    A lazy argument runs the caller work per walk, so one materialisation makes the cost predictable.

  9. Why must a query over a file reader be walked inside the using block?

    Answer

    The source is closed when the block ends, and a later walk fails

    Deferred execution reads the source at the walk, so the walk must happen while the source is open.

  10. What does frozen.Count work on the result of ToList but frozen.Count() work on a query?

    Answer

    Count is a property of List<T>, Count() is the LINQ method for any sequence

    A list knows its size as a property, while a lazy query has to be walked by the Count() method.

The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev