With LINQ to Objects, a Where query built before an order is added is then counted and looped over; what do Count() and foreach see?
answer
- the query variable holds no results
- Where returns a stored recipe
- Count() must produce a number now
- every pass reads the list again
basics
~20 sBoth see the added order. Where only stores the source and the predicate; Count() runs the filter over the list as it is at that moment, and foreach later runs it again from the start.
solid answer
~40 s`orders.Where(o => o.Total > 100m)` is **deferred**: it returns an iterator that holds a reference to the list and the predicate, and nothing is filtered on that line. `Count()` is **immediate** because it returns an `int`, so it enumerates the query when it is called and reads the list including the order added after the query was declared. The later `foreach` enumerates the same query a second time and calls the predicate once per element again. As a rule, the operators that return `IEnumerable<T>` or `IOrderedEnumerable<T>` (`Where`, `Select`, `OrderBy`) defer, while those returning a single value or a container (`Count`, `First`, `Sum`, `Any`, `ToList`, `ToArray`) run at once. To freeze one result, call `ToList()` once and read the list's `Count` property.
code
csharp · 19 linesvar orders = new List<Order> { new(1, 250m), new(2, 40m) };
int predicateCalls = 0;
var large = orders.Where(o => { predicateCalls++; return o.Total > 100m; });
Console.WriteLine($"After building: {predicateCalls} predicate calls");
orders.Add(new Order(3, 900m)); // added after the query was built
Console.WriteLine($"Count() = {large.Count()}, calls so far: {predicateCalls}");
foreach (var o in large)
Console.WriteLine($"Order {o.Id}: {o.Total}");
Console.WriteLine($"After foreach: {predicateCalls} predicate calls");
var snapshot = large.ToList(); // runs the filter a third time, then stores results
orders.Add(new Order(4, 500m));
Console.WriteLine($"Snapshot {snapshot.Count}, live query {large.Count()}");
record Order(int Id, decimal Total);go deeper
Recall that a LINQ query built with Where or Select does nothing until something enumerates it, and that Count(), First() and ToList() are calls that make it run.
Explain deferred versus immediate operators, why the late order is counted, and why Count() followed by foreach runs the predicate twice, distinguishing Enumerable.Count() from List<T>.Count.
Spot code that counts and then loops over the same query, decide where a snapshot belongs, and explain the exception raised when the source changes mid-enumeration.
Weigh when a live, re-runnable query is the intended behaviour and when a team should require explicit materialisation so results stay consistent across reads.
## What a query variable holds In LINQ to Objects (LINQ over in-memory `IEnumerable<T>` sources such as `List<T>` and arrays), a line like ```csharp var large = orders.Where(o => o.Total > 100m); ``` does **not** filter anything. `Enumerable.Where` checks its arguments, then returns an **iterator object** that remembers two things: a reference to `orders` and the predicate delegate. The variable `large` therefore holds a *recipe* for producing results, not the results. Microsoft's documentation puts it plainly: the query variable itself takes no action and returns no data. The recipe runs when something **enumerates** it: a `foreach`, or an operator that needs actual elements to produce its answer. This is **deferred execution**, and it has a direct consequence: the query reads its source as the source is *at enumeration time*, not as it was when the query was declared. The same holds for query syntax. `from o in orders where o.Total > 100m select o` is translated by the compiler into a call to `Where`, so it is exactly as deferred. Chaining more operators, such as `.Select(...)` or `.OrderBy(...)`, wraps one iterator in another; the chain still does nothing until the outermost result is enumerated. ## Deferred and immediate operators The standard query operators split by what they return. | Kind | Typical operators | When work happens | |---|---|---| | **Deferred** | `Where`, `Select`, `SelectMany`, `OrderBy`, `GroupBy`, `Skip`, `Take`, `Concat` | when the result is enumerated, and again on every enumeration | | **Immediate** | `Count`, `Sum`, `Max`, `Average`, `First`, `Single`, `Any`, `Contains`, `ToList`, `ToArray`, `ToDictionary` | at the call, once | A useful rule of thumb: an operator that returns `IEnumerable<T>` or `IOrderedEnumerable<T>` is deferred; one that returns a scalar (`int`, `bool`, `decimal`, an element) or a filled container is immediate, because it cannot hand back its answer without reading the data. ## Walking through the report service The example below counts how often the predicate runs. Read the output against the code: 1. **After building** the query, the predicate has run **0** times. `Where` stored it and returned. 2. An order of 900 is **added** to the list. The query does not notice anything, because it has not run yet. 3. **`Count()`** enumerates the query: three orders, three predicate calls, result **2** (250 and the new 900). The late order is included because the list is read now. 4. **`foreach`** enumerates the same query again from scratch: three more predicate calls, total **6**. Nothing was cached by `Count()`. 5. **`ToList()`** runs the pipeline a third time and stores the matches in a new `List<Order>`. After another order is added, the snapshot still holds **2** while the live query now counts **3**. The point interviewers look for is step 4: a deferred query is re-executed on every enumeration. If the predicate were expensive, or logged, or called a service, that work would be repeated. ## `Enumerable.Count()` versus `List<T>.Count` Two different members share the name: - **`List<T>.Count`** is a property that reads a stored field. It never enumerates. - **`Enumerable.Count()`** is an extension method on `IEnumerable<T>`. It first checks whether the source implements `ICollection<T>`; if so, it returns that collection's `Count` without enumerating. A `Where` query is not a collection, so `Count()` walks every element and runs the predicate for each. So `orders.Count()` on a plain list is cheap, while `large.Count()` on a filtered query costs a full pass, and calling it and then looping costs two. ## Freezing a result when you need one Deferred execution is useful when you *want* the latest data each time, for example a query re-run on a timer. When you want one consistent answer instead: - Call `ToList()` or `ToArray()` once, at the point that should define the snapshot. - Use the stored list for every later read: its `Count` property, indexing and `foreach` no longer touch the predicate. - Do not add to the source *while* a `foreach` over the query is running: the list's enumerator detects the change and throws `InvalidOperationException` on the next step. ## Summary - Building a LINQ to Objects query stores a source reference and delegates; it does not run them. - Immediate operators such as `Count()` run the query when called and see the source's current contents. - Each enumeration runs the whole pipeline again; nothing is cached unless you materialise. - `ToList()` or `ToArray()` turns the live query into a snapshot that later changes to the source do not affect.
- Does Enumerable.Count() always enumerate its source?No. `Count()` first checks for `ICollection<T>` and, when the source is a `List<T>` or an array, returns the stored count without enumerating. A `Where` or `Select` query is not a collection, so `Count()` walks it; for a `Select`, .NET's implementation still invokes the selector on each element even when the length is known, because callers sometimes use `Count()` to force those side effects. `Enumerable.TryGetNonEnumeratedCount` reports a count only when one is available without enumerating.
- What happens if the loop adds an order to the list while it is enumerating the Where query?The `List<T>` enumerator underneath the `Where` iterator checks the list's version on every step. After an `Add`, the next `MoveNext` throws `InvalidOperationException` with "Collection was modified; enumeration operation may not execute." The query does not take a copy, so writing to the source during a pass is an error rather than a way to see new items. Materialise first with `ToList()` when the loop must add.
- Where filters the list on the line that declares the query
- Count() reports the matches that existed when the query was declared
- A query variable caches its results after the first foreach
- Enumerable.Count() is always free because it reads a stored count
- Items added to the source after building the query are ignored