skip to content

In .NET 10, what does System.Linq.AsyncEnumerable add for IAsyncEnumerable<T>, and what changes for code built on the System.Linq.Async package?

level: middleimportance: should knowfreq 30%

answer

  1. LINQ for async sequences, in the box
  2. terminal operators end in Async
  3. SelectAwait folds into Select
  4. ambiguous calls after upgrading

basics

~20 s

.NET 10 ships System.Linq.AsyncEnumerable: Where, Select, Take and the rest for IAsyncEnumerable<T>, plus terminal operators like ToListAsync that return ValueTask. It replaces the community System.Linq.Async package, which can cause ambiguity errors until removed or upgraded.

solid answer

~40 s

Before .NET 10, LINQ over `IAsyncEnumerable<T>` came from the community `System.Linq.Async` package. .NET 10 puts a `System.Linq.AsyncEnumerable` class in the shared framework (also published as the `System.Linq.AsyncEnumerable` package for older targets): streaming operators such as `Where`, `Select`, `Take` and `Chunk` return `IAsyncEnumerable<T>`, and terminal operators such as `ToListAsync`, `CountAsync` and `FirstAsync` return `ValueTask` and take a `CancellationToken`. Async predicates and selectors are overloads of the same names, taking `Func<T, CancellationToken, ValueTask<TResult>>`, so `SelectAwait` becomes `Select`. Upgrading a project that references the old package can produce ambiguous-call errors: remove it, move to its 7.0.0 release, or exclude its compile assets when it arrives transitively. Watch one trap: a one-parameter `async` lambda binds to the synchronous `Select` and yields a stream of `Task<TResult>`.

code

csharp · 29 lines
csharp
// .NET 10: System.Linq.AsyncEnumerable is part of the shared framework
List<string> alerts = await TailAsync()
    .Where(line => line.StartsWith("WARN"))
    .Select(async (string line, CancellationToken ct) =>
    {
        await Task.Delay(5, ct);                  // async work per item
        return line.ToUpperInvariant();
    })
    .Take(2)
    .ToListAsync();                               // ValueTask<List<string>>
Console.WriteLine(string.Join(" | ", alerts));

// One-parameter async lambda binds to Select(Func<TSource, TResult>)
IAsyncEnumerable<Task<int>> lengths = TailAsync().Select(async line =>
{
    await Task.Delay(1);
    return line.Length;
});
Console.WriteLine(await lengths.CountAsync());   // a stream of tasks, not ints

static async IAsyncEnumerable<string> TailAsync()
{
    string[] page = ["INFO started", "WARN disk 91%", "INFO tick", "WARN disk 95%", "WARN disk 99%"];
    foreach (string line in page)
    {
        await Task.Delay(10);
        yield return line;
    }
}

go deeper

for a junior

Remember that .NET 10 ships LINQ operators for IAsyncEnumerable<T> in the box, and that terminal operators like ToListAsync end in Async.

for a middle

Explain the API shape: streaming operators return async sequences, terminal ones return ValueTask, and async callbacks are overloads taking a CancellationToken.

for a senior

Spot the one-parameter async lambda that yields a stream of tasks, and know how to resolve System.Linq.Async ambiguity errors during an upgrade.

for a principal

Plan the migration for a codebase: drop or upgrade the community package, multitarget with the new package, and sweep SelectAwait call sites.

## What shipped in .NET 10 `IAsyncEnumerable<T>` has existed since .NET Core 3.0, but the base class library never shipped LINQ operators for it. Teams used the community-maintained **`System.Linq.Async`** NuGet package, which defined its own `System.Linq.AsyncEnumerable` class. .NET 10 brings the operators into the platform: **`System.Linq.AsyncEnumerable`** is part of the shared framework, and the same API is published as the `System.Linq.AsyncEnumerable` NuGet package for projects that still target older frameworks. The surface mirrors `Enumerable` with async-aware twists: - **Streaming operators** keep their familiar names and return `IAsyncEnumerable<T>`: `Where`, `Select`, `SelectMany`, `Take`, `Skip`, `Chunk`, `Distinct`, `OrderBy`, `GroupBy`, `Join` and more. - **Terminal operators** end in `Async`, return `ValueTask` or `ValueTask<T>`, and take an optional `CancellationToken`: `ToListAsync` (a `ValueTask<List<T>>`), `ToArrayAsync`, `CountAsync`, `AnyAsync`, `FirstAsync`, `SumAsync` and the rest. - **Async callbacks** are overloads of the same operator: `Where` accepts `Func<TSource, CancellationToken, ValueTask<bool>>` and `Select` accepts `Func<TSource, CancellationToken, ValueTask<TResult>>`, alongside the plain synchronous delegates. - **Bridges and sources**: `ToAsyncEnumerable()` turns an `IEnumerable<T>` into an async sequence, and `Empty`, `Range` and `Repeat` create one directly. The example below filters a log tail with `Where`, transforms each entry with an async `Select`, takes two and buffers them with `ToListAsync()`, with no package reference at all. ## What changes for System.Linq.Async users The official breaking-change note classifies this as a **source** break: code that references `System.Linq.Async` and targets .NET 10 can hit ambiguous-call compile errors, because two classes with the same name and overlapping extension methods are now in scope. The recommended actions are: 1. **Direct reference:** remove the `System.Linq.Async` package reference, or upgrade it to version 7.0.0, the release the note names for .NET 10. 2. **Multitargeting** .NET 10 and an older framework: reference the `System.Linq.AsyncEnumerable` package instead, so every target uses the same API. 3. **Transitive reference** through another package: add `<ExcludeAssets>compile</ExcludeAssets>` (or `all`, if nothing needs it at run time) to keep its extension methods out of your code. Most call sites compile unchanged. The ones that need edits used the old package's separate names for async callbacks: `SelectAwait(async (int i, CancellationToken ct) => ...)` becomes `Select(async (int i, CancellationToken ct) => ...)`; async predicates likewise go to the `Where` overload that takes a `CancellationToken` and returns `ValueTask<bool>`. ## The overload trap with async lambdas Because the async and sync callbacks share a name, overload resolution picks by the lambda's shape: | Lambda | Overload chosen | Result type | |---|---|---| | `x => x.Length` | `Select(Func<TSource, TResult>)` | `IAsyncEnumerable<int>` | | `async x => await F(x)` | `Select(Func<TSource, TResult>)` with `TResult = Task<int>` | `IAsyncEnumerable<Task<int>>` | | `async (x, ct) => await F(x, ct)` | `Select(Func<TSource, CancellationToken, ValueTask<TResult>>)` | `IAsyncEnumerable<int>` | A one-parameter `async` lambda compiles, but it produces a stream of **unawaited tasks**: each call starts when its element is selected, the pipeline moves on without waiting for it, and unless the consumer awaits every task itself, their results and exceptions go unobserved. The fix is to take the `CancellationToken` parameter, which picks the async overload and lets the work be cancelled. The example below prints a count of five tasks from exactly this mistake. ## Older targets Library authors who still ship for older frameworks are not left out. The `System.Linq.AsyncEnumerable` package builds for the previous .NET releases, for .NET Standard 2.0 and for .NET Framework, so the same calls compile everywhere: on .NET 10 the class is already in the framework, and on older targets the package supplies it. That is why the breaking-change note recommends it, rather than the community package, for any project that multitargets .NET 10 and an earlier release. ## Interview angles - **Why it matters:** LINQ over async sequences was an extra dependency with its own naming; now it is part of the platform, and a team can drop the package. - **Return types:** terminal operators return `ValueTask`, so the usual consumption rules apply: await each result once, which the material on `ValueTask` covers in depth. - **Cancellation:** pass the token to the terminal operator, for example `ToListAsync(token)`, or use `WithCancellation` on the sequence; the iterator at the source still needs `[EnumeratorCancellation]` to see it. - **Upgrades:** expect ambiguity errors from a stale package before any behaviour change, and know the three fixes above. What the operators do one by one, deferred versus buffering behaviour and the meaning of `GroupBy` or `Join`, is the same as for LINQ to Objects and is covered there; the async version differs in its terminal operators, its callback overloads and its packaging.

  • Why do the terminal operators return ValueTask rather than Task?
    Performance: many steps of an async sequence complete synchronously, and `ValueTask<T>` can avoid allocating a `Task` when the result is already available, matching `IAsyncEnumerator<T>.MoveNextAsync()`, which also returns `ValueTask<bool>`. The cost is the usual `ValueTask` discipline: await the result once and do not store it for later.
  • How do you call an async method per element and keep the results as values, not tasks?
    Use the overload whose selector takes a `CancellationToken` and returns a `ValueTask<TResult>`: `source.Select(async (item, ct) => await LoadAsync(item, ct))`. Each element's call is awaited in turn, so the sequence yields results. A one-parameter `async` lambda instead binds to the synchronous overload and yields `Task<TResult>` objects that nothing awaits.

  • LINQ over IAsyncEnumerable<T> on .NET 10 still requires the System.Linq.Async package.
  • The .NET 10 operators keep a separate SelectAwait method for async selectors.
  • ToListAsync in .NET 10 returns Task<List<T>>.
  • Select(async x => await F(x)) yields awaited results on .NET 10.
  • Upgrading to .NET 10 changes behaviour silently but never breaks compilation.

Assumes .NET 10, which added System.Linq.AsyncEnumerable to the shared framework; earlier targets need the System.Linq.AsyncEnumerable or System.Linq.Async package.