// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information. using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace System.Linq { public static partial class AsyncEnumerable { public static Task Sum(this IAsyncEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func> selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } private static async Task SumCore(IAsyncEnumerable source, CancellationToken cancellationToken) { var sum = 0; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { checked { sum += e.Current; } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { var sum = 0; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = selector(e.Current); checked { sum += value; } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { var sum = 0; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await selector(e.Current).ConfigureAwait(false); checked { sum += value; } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } public static Task Sum(this IAsyncEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func> selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } private static async Task SumCore(IAsyncEnumerable source, CancellationToken cancellationToken) { var sum = 0L; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { checked { sum += e.Current; } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { var sum = 0L; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = selector(e.Current); checked { sum += value; } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { var sum = 0L; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await selector(e.Current).ConfigureAwait(false); checked { sum += value; } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } public static Task Sum(this IAsyncEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func> selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } private static async Task SumCore(IAsyncEnumerable source, CancellationToken cancellationToken) { var sum = 0.0f; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { sum += e.Current; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { var sum = 0.0f; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = selector(e.Current); sum += value; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { var sum = 0.0f; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await selector(e.Current).ConfigureAwait(false); sum += value; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } public static Task Sum(this IAsyncEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func> selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } private static async Task SumCore(IAsyncEnumerable source, CancellationToken cancellationToken) { var sum = 0.0; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { sum += e.Current; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { var sum = 0.0; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = selector(e.Current); sum += value; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { var sum = 0.0; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await selector(e.Current).ConfigureAwait(false); sum += value; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } public static Task Sum(this IAsyncEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func> selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } private static async Task SumCore(IAsyncEnumerable source, CancellationToken cancellationToken) { var sum = 0m; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { sum += e.Current; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { var sum = 0m; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = selector(e.Current); sum += value; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { var sum = 0m; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await selector(e.Current).ConfigureAwait(false); sum += value; } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } public static Task Sum(this IAsyncEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func> selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } private static async Task SumCore(IAsyncEnumerable source, CancellationToken cancellationToken) { var sum = 0; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { checked { sum += e.Current.GetValueOrDefault(); } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { var sum = 0; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = selector(e.Current); checked { sum += value.GetValueOrDefault(); } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { var sum = 0; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await selector(e.Current).ConfigureAwait(false); checked { sum += value.GetValueOrDefault(); } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } public static Task Sum(this IAsyncEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func> selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } private static async Task SumCore(IAsyncEnumerable source, CancellationToken cancellationToken) { var sum = 0L; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { checked { sum += e.Current.GetValueOrDefault(); } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { var sum = 0L; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = selector(e.Current); checked { sum += value.GetValueOrDefault(); } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { var sum = 0L; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await selector(e.Current).ConfigureAwait(false); checked { sum += value.GetValueOrDefault(); } } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } public static Task Sum(this IAsyncEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func> selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } private static async Task SumCore(IAsyncEnumerable source, CancellationToken cancellationToken) { var sum = 0.0f; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { sum += e.Current.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { var sum = 0.0f; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = selector(e.Current); sum += value.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { var sum = 0.0f; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await selector(e.Current).ConfigureAwait(false); sum += value.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } public static Task Sum(this IAsyncEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func> selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } private static async Task SumCore(IAsyncEnumerable source, CancellationToken cancellationToken) { var sum = 0.0; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { sum += e.Current.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { var sum = 0.0; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = selector(e.Current); sum += value.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { var sum = 0.0; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await selector(e.Current).ConfigureAwait(false); sum += value.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } public static Task Sum(this IAsyncEnumerable source) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); return SumCore(source, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } public static Task Sum(this IAsyncEnumerable source, Func> selector) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, CancellationToken.None); } public static Task Sum(this IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { if (source == null) throw new ArgumentNullException(nameof(source)); if (selector == null) throw new ArgumentNullException(nameof(selector)); return SumCore(source, selector, cancellationToken); } private static async Task SumCore(IAsyncEnumerable source, CancellationToken cancellationToken) { var sum = 0m; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { sum += e.Current.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func selector, CancellationToken cancellationToken) { var sum = 0m; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = selector(e.Current); sum += value.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } private static async Task SumCore(IAsyncEnumerable source, Func> selector, CancellationToken cancellationToken) { var sum = 0m; var e = source.GetAsyncEnumerator(cancellationToken); try { while (await e.MoveNextAsync().ConfigureAwait(false)) { var value = await selector(e.Current).ConfigureAwait(false); sum += value.GetValueOrDefault(); } } finally { await e.DisposeAsync().ConfigureAwait(false); } return sum; } } }