With LINQ to Objects over a large order export, why does Where followed by First stop early while OrderByDescending followed by Take(1) reads every row?
answer
- deferred does not mean streaming
- a sort needs every key
- buffered on the first MoveNext
- GroupBy and Reverse behave alike
basics
~20 sWhere streams: it yields each match as it reads it, so First stops at the first one. OrderByDescending is deferred but nonstreaming: on its first MoveNext it copies the whole source into an array and sorts before yielding anything.
solid answer
~40 sBoth operators are **deferred**, but Microsoft's documentation splits deferred operators into **streaming** and **nonstreaming**. `Where`, `Select`, `Take`, `Skip` and `SelectMany` stream: each result is produced as soon as the source elements it needs have been read, so `First()` can stop the whole pipeline early. `OrderBy`, `OrderByDescending`, `ThenBy`, `GroupBy` and `Reverse` are nonstreaming: the first time the result is enumerated, the .NET implementation reads the entire source into a buffer (an array for sorting and reversing, a lookup for grouping) and only then yields. `Take(1)` after the sort cannot help, because the sort has already read every row. The consequences are memory proportional to the input, a delay before the first item, and an operator that never yields on an unbounded source.
code
csharp · 19 linesint reads = 0;
IEnumerable<Order> ReadOrders() // stands in for a large export read row by row
{
for (int id = 1; id <= 100_000; id++) { reads++; yield return new Order(id, id % 997); }
}
var firstLarge = ReadOrders().Where(o => o.Total > 900m).First();
Console.WriteLine($"Where + First: order {firstLarge.Id}, {reads} rows read");
reads = 0;
var top = ReadOrders().OrderByDescending(o => o.Total).Take(1).ToList();
Console.WriteLine($"OrderByDescending + Take(1): order {top[0].Id}, {reads} rows read");
reads = 0;
using var e = ReadOrders().GroupBy(o => o.Total > 500m).GetEnumerator();
e.MoveNext(); // ask for the first group only
Console.WriteLine($"GroupBy, first group: {reads} rows read");
record Order(int Id, decimal Total);go deeper
Recall that Where and Select hand out results as they go, while sorting needs to see every item before it can return the first one.
Explain the streaming and nonstreaming split among deferred operators and name OrderBy, GroupBy and Reverse as the ones that buffer their whole source.
Predict memory and time-to-first-result for a pipeline over a large or endless source, reorder stages so filters run before sorts, and recognise when a buffer hides source changes.
Judge which sorting and grouping work belongs in memory at all, and set expectations for pipelines whose input size is not bounded.
## Deferred has two flavours Every LINQ to Objects operator that returns a sequence is **deferred**: it does nothing until enumerated. Microsoft's documentation adds a second distinction among deferred operators: - **Streaming** operators do not need to read all the source before producing a result. They process each source element as it arrives and yield a result as soon as one is ready. More than one source element may be read to produce one result, as `Where` skipping non-matches shows. - **Nonstreaming** operators must read **all** the source data before they can yield any result. Sorting and grouping are the classic cases: the smallest key might be the last row. Deferred answers *when* an operator starts; streaming answers *how much* it reads before its first result. ## What the export example shows The example below reads a simulated export of 100,000 orders through an iterator that counts rows as they are produced. 1. **`Where(o => o.Total > 900m).First()`** reads **901** rows. `First()` asks for one element; `Where` pulls rows until one matches and hands it over; `First()` then stops and the iterator is disposed. Nothing after row 901 is ever produced. 2. **`OrderByDescending(o => o.Total).Take(1).ToList()`** reads **all 100,000** rows. When `Take` asks the sorted sequence for its first element, the ordering iterator calls `ToArray()` on its source, computes the keys and orders them (.NET can use a partial sort when only the first few positions are wanted), and only then yields. `Take(1)` limits what comes *out*, not what the sort reads. 3. **`GroupBy(o => o.Total > 500m)`**, enumerated just far enough to get the first group, also reads **all 100,000** rows, because the first group might gain members from the last row. The implementation builds a lookup from the entire source on the first `MoveNext`. ## Which operators buffer | Operator | Flavour | What it holds while running | |---|---|---| | `Where`, `Select`, `SelectMany` | streaming | one element at a time | | `Take`, `Skip`, `TakeWhile`, `SkipWhile` | streaming | a counter or a flag | | `Concat`, `Cast`, `OfType` | streaming | one element at a time | | `Distinct`, `Union` | streaming | a set of the elements seen so far | | `OrderBy`, `OrderByDescending`, `ThenBy` | nonstreaming | an array of every element plus their keys | | `GroupBy` | nonstreaming | a lookup holding every element | | `Reverse` | nonstreaming | an array of every element | | `Join`, `GroupJoin` | streams the outer sequence | a lookup built from the whole inner sequence | The last two rows of nuance matter: `Distinct` streams but its memory grows with the number of distinct elements, and `Join` streams the first sequence while fully buffering the second. ## Consequences in production - **Memory.** Sorting a million-row export holds all million elements and their keys in arrays at once, even if the caller wants ten. - **Latency to first result.** A streaming pipeline can print its first line after a few rows; a sorted one prints nothing until the whole source has been read and sorted. - **Unbounded sources.** `OrderBy` over an endless generator never yields; `Where(...).Take(5)` over the same generator finishes. - **Source changes after the buffer is taken.** Because a buffering operator works on its own copy, adding to the source list during a `foreach` over `list.OrderBy(...)` does not throw; the loop simply never sees the addition. A streaming `Where` over the same list would throw `InvalidOperationException`. ## Placing stages deliberately 1. **Filter before you sort**: `Where(...).OrderBy(...)` sorts only the survivors; `OrderBy(...).Where(...)` sorts everything first. 2. **Remember what `Take` after `OrderBy` means**: it is "the top N", and the sort must still see every row to know them. 3. **For the single largest or smallest element**, a one-pass operator such as `MaxBy` (covered with grouping and projection operators) avoids the buffer; .NET's implementation can also turn `OrderBy(...).First()` into a single scan that keeps the best key, but that still reads every row. 4. **When the source is huge**, consider whether the sort belongs at the data source rather than in memory. ## Summary - Deferred says when an operator starts; streaming says whether it can yield before reading everything. - `Where`, `Select`, `Take` and friends stream, so `First()` can stop early. - `OrderBy`, `GroupBy` and `Reverse` read the whole source into a buffer on their first `MoveNext`. - `Take` after a sort limits output, not input.
- Does OrderBy(...).First() sort the whole sequence?It does not have to sort, but it still reads everything. .NET's implementation recognises `First()` on an ordered sequence and can replace the sort with a single pass that keeps the element with the best key seen so far, which is linear rather than a full sort. It cannot stop early, though: the smallest key might be in the last row, so every element is read and keyed.
- Why does adding to a list inside foreach over list.OrderBy(...) not throw, when the same loop over list.Where(...) does?On its first `MoveNext`, the ordering iterator copies the source into its own array and from then on reads only that array, so nothing is still reading the list when the loop body adds to it and no version check can fail; the new item is simply not seen. `Where` keeps the list's enumerator open for the whole loop, so its next `MoveNext` detects the change and throws `InvalidOperationException`.
- Every deferred operator processes one element at a time
- OrderBy sorts the list on the line that declares the query
- Take(1) after OrderBy lets the sort stop after one row
- GroupBy yields the first group before reading the rest of the source
- A streaming operator reads exactly one source element per result