i3arnon / MongoAsyncEnumerableAdapter

Provides an adapter from MongoDB's IAsyncCursor & IAsyncCursorSource to IAsyncEnumerable
MIT License
20 stars 5 forks source link

MongoAsyncEnumerableAdapter

NuGet NuGet license

Provides an adapter from MongoDB's IAsyncCursor<TDocument> and IAsyncCursorSource<TDocument> to IAsyncEnumerable<T>

This allows plugging MongoDB's custom async iterators into the async LINQ ecosystem by wrapping IAsyncCursorSource<TDocument> or IAsyncCursor<TDocument> in an IAsyncEnumerable<T> implementation.

For example, iterating on a find result with await foreach:

IMongoCollection<Hamster> collection = // ...
IFindFluent<Hamster, Hamster> findFluent = collection.Find(hamster => hamster.Name == "bar");

await foreach (var hamster in findFluent.ToAsyncEnumerable())
{
    Console.WriteLine(hamster.Age);
}

Or any other async LINQ operator:

IMongoCollection<Hamster> collection = // ...

int groupCount = 
    await collection.AsQueryable().
        ToAsyncEnumerable().
        GroupBy(hamster => hamster.Age).
        CountAsync();