Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท LINQ

LINQ for IAsyncEnumerable<T>

Reference for LINQ over IAsyncEnumerable<T> in C#: Where, Select, ToListAsync, async lambdas, cancellation, and the System.Linq.Async package versus .NET 10.

IAsyncEnumerable<T> is a sequence whose elements arrive asynchronously. You produce one with an async iterator method (async IAsyncEnumerable<T> plus yield return) and consume it with await foreach. For a long time the standard LINQ operators did not work on it; the community package System.Linq.Async filled the gap.

Starting with .NET 10 the operators are part of the base class library, in System.Linq.AsyncEnumerable. You can call Where(), Select(), Take(), ToListAsync(), CountAsync(), SumAsync() and many more directly on an IAsyncEnumerable<T> without adding a package. Operators that produce a single value are async methods you await; operators that produce a sequence stay lazy.

C#
var top = await source.Where(x => x.Ok).Take(10).ToListAsync();

Try it

C# Query an async stream

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Async lambdas and cancellation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
// namespace System.Linq, static class AsyncEnumerable (.NET 10)
public static IAsyncEnumerable<TSource> Where<TSource>(
    this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate);

public static IAsyncEnumerable<TResult> Select<TSource, TResult>(
    this IAsyncEnumerable<TSource> source,
    Func<TSource, CancellationToken, ValueTask<TResult>> selector);

public static ValueTask<List<TSource>> ToListAsync<TSource>(
    this IAsyncEnumerable<TSource> source, CancellationToken cancellationToken = default);

public static IAsyncEnumerable<TSource> ToAsyncEnumerable<TSource>(
    this IEnumerable<TSource> source);
Name Description
source The async sequence to query.
predicate, selector Lambdas for Where() and Select(). Overloads exist for synchronous lambdas and for async ones that receive a CancellationToken and return a ValueTask<T>.
cancellationToken Cancels the terminal operator. For await foreach use .WithCancellation(token).

Returns: sequence operators return another IAsyncEnumerable<T> (lazy); terminal operators (ToListAsync, CountAsync, SumAsync, FirstAsync, AnyAsync, AggregateAsync ...) return a ValueTask or ValueTask<T> you must await.

Errors: the same as the synchronous operators (InvalidOperationException for FirstAsync() on an empty sequence), delivered when you await. Exceptions in the producer surface during enumeration.

Usage notes

  • .NET 10 and the package. On .NET 10 and later, System.Linq.AsyncEnumerable is built in. On older targets add the NuGet package System.Linq.Async; the method names are the same. Both providing the same methods can cause ambiguity errors when you reference the package from .NET 10 code, so remove the package when you upgrade.
  • Async lambdas. In .NET 10 the async versions are overloads of the same names (the package called them SelectAwait(), WhereAwait() and so on). Inference can report an ambiguity (CS0121) between the index overload and the async overload, so give explicit type arguments: Select<int, string>(async (n, ct) => ...).
  • Lazy until consumed. Nothing is produced until await foreach or a terminal operator pulls items, and each enumeration starts the producer again.
  • Streaming memory use. Where(), Select() and Take() hold one element at a time. OrderBy(), GroupBy() and ToListAsync() buffer everything.
  • Cancellation tokens. Mark the token parameter of your iterator with [EnumeratorCancellation] so WithCancellation() reaches it.
  • Not the same as PLINQ or Task.WhenAll(). Elements are still produced one after another; this is for sequences that wait, not for parallel work.
  • Disposal. await foreach disposes the enumerator, which runs finally blocks of the iterator; stopping early with break or Take() is safe.
  • Common mistake: returning Task<IEnumerable<T>> when you really want a stream; the caller then waits for the whole list before seeing the first item.