LLearnCode
Academy/csharp/Collections and behavior

intermediate · 29 minute study

A LINQ query can be a recipe rather than a snapshot

Read data pipelines without accidentally rerunning work or assuming that a deferred query already froze its result.

Loading progressEvidence grows with practice
Useful preparation · 1 prerequisite

Use this as a map, not a lock. If these ideas are familiar, continue.

First, build a mental model

Where and Select on an IEnumerable commonly build a recipe. Enumeration performs the work. ToList materializes a result at that moment; later source mutations do not add elements to that list.

  1. Where filters elements; Select transforms each retained element; GroupBy organizes groups for later work.
  2. Deferred execution means declaring a query and executing it can happen at different times.
  3. Materializing with ToList captures current result elements, though referenced mutable elements can still be shared.
  4. IQueryable providers may translate expression trees to another system such as SQL. An operation supported in memory is not automatically translatable or efficient on a database.
Read the codecsharp · educational example
var scores = new List<int> { 10, 30 };
var query = scores.Where(score => score >= 20);
scores.Add(40);
var snapshot = query.ToList();
scores.Add(50);
Console.WriteLine(string.Join(",", snapshot));
Console.WriteLine(string.Join(",", query));
  1. Declare

    query describes the filter; it has not created an independent result list.

  2. Materialize

    ToList runs after 40 is added, producing 30 and 40.

  3. Enumerate again

    The final enumeration sees the source's later 50 as well.

Production thinking

Take this beyond the example.

Avoid repeated enumeration over expensive sources, hidden per-row I/O and accidental unbounded materialization. Measure result size and translation at the actual provider.

Sources and further reading

Your evidence

unseen is a local learning signal, not a credential or proof of production competence.

Explain the mental model, then explicitly acknowledge it. Opening a page is not evidence.

Supported retries can complete this lesson. Independent evidence and delayed review determine the skill stage.

Next lesson · Model absence before deciding whether to throw →