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.
Useful preparation · 1 prerequisite
Use this as a map, not a lock. If these ideas are familiar, continue.
- Functions can be values, and subscriptions have lifetimes — Understand callbacks, notifications and event-related memory leaks before building a reactive UI or game system.
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.
- Where filters elements; Select transforms each retained element; GroupBy organizes groups for later work.
- Deferred execution means declaring a query and executing it can happen at different times.
- Materializing with ToList captures current result elements, though referenced mutable elements can still be shared.
- 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.
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));- Declare
query describes the filter; it has not created an independent result list.
- Materialize
ToList runs after 40 is added, producing 30 and 40.
- 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 →