Skip to content

yanivru/EnumerateAnd

Repository files navigation

EnumerateAnd

Performs multiple queries while enumerating the IAsyncEnumerable only once.

Usage

In the following example the sourceEnumerable is enumerated only once even though we execute both AnyAsync and CountAsync, both from System.Linq.Async package.

            var sourceEnumerable = AsyncEnumerable.Range(1, 10);

            (var any, var count) = await sourceEnumerable
                .QueryAnd(x => x.AnyAsync())
                .QueryAsync(x => x.CountAsync());

            Assert.IsTrue(any);
            Assert.AreEqual(10, count);

Any method that accepts IAsyncEnumerable as a parameter can be used:

            var sourceEnumerable = AsyncEnumerable.Range(1, 10);

            (_, var count) = await sourceEnumerable.QueryAnd(x => DoSomethingAsync(x))
                .QueryAsync(x => x.CountAsync());

            Assert.AreEqual(10, count);

            private static async ValueTask<bool> DoSomethingAsync(IAsyncEnumerable<int> x)
            {
                await foreach(var item in x)
                {
                  Console.WriteLine(item);
                }
  
                // It must return something (There is now support for async Actions for now).
                return true;
            }

Up to 3 queries can be run on the IAsyncEnumerable.

About

Easily multi-query IAsyncEnumerable with no multi-enumeration issues

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages