Skip to content
On this page

Querying to IAsyncEnumerable


See Iterating with Async Enumerables in C# 8 for more context around async enumerables

Marten V4.0 introduced a custom Linq operator to return the results of Linq queries to an IAsyncEnumerable<T>. This can be very valuable when you expect large data sets because it allows you to process the documents being read by Marten in memory while Marten is still fetching additional results and avoids the need to ever put the entire document result set into memory.

The simple addition to Marten is the IQueryable<T>.ToAsyncEnumerable() and IMartenQueryable<T>.ToAsyncEnumerable()` extension methods. Below is a sample usage of this new operator from the Marten tests:

public async Task query_to_async_enumerable()
    var targets = Target.GenerateRandomData(20).ToArray();
    await theStore.BulkInsertAsync(targets);

    var ids = new List<Guid>();

    var results = theSession.Query<Target>()

    await foreach (var target in results)

    foreach (var target in targets)

snippet source | anchor

Released under the MIT License.