namespace System.Linq; public static class AsyncIEnumerableExtensions { /// /// Projects each element of an async-enumerable sequence into a new form by applying /// an asynchronous selector function to each member of the source sequence and awaiting /// the result. /// /// The type of the elements in the source sequence /// /// The type of the elements in the result sequence, obtained by running the selector /// function for each element in the source sequence and awaiting the result. /// /// A sequence of elements to invoke a transform function on /// An asynchronous transform function to apply to each source element /// /// An async-enumerable sequence whose elements are the result of invoking the transform /// function on each element of the source sequence and awaiting the result /// public static IAsyncEnumerable SelectAwait(this IEnumerable source, Func> predicate) { return source.ToAsyncEnumerable().SelectAwait(predicate); } /// /// Returns the first element of an async-enumerable sequence that satisfies the /// condition in the predicate, or a default value if no element satisfies the condition /// in the predicate /// /// The type of element in the sequence /// Source sequence /// An asynchronous predicate to invoke and await on each element of the sequence /// /// A Task containing the first element in the sequence that satisfies the predicate, /// or a default value if no element satisfies the predicate /// /// A task that represents the asynchronous operation public static Task FirstOrDefaultAwaitAsync(this IEnumerable source, Func> predicate) { return source.ToAsyncEnumerable().FirstOrDefaultAwaitAsync(predicate).AsTask(); } /// /// Determines whether all elements in an async-enumerable sequence satisfy a condition /// /// The type of element in the sequence /// An sequence whose elements to apply the predicate to /// An asynchronous predicate to apply to each element of the source sequence /// /// A Task containing a value indicating whether all elements in the sequence /// pass the test in the specified predicate /// /// A task that represents the asynchronous operation public static Task AllAwaitAsync(this IEnumerable source, Func> predicate) { return source.ToAsyncEnumerable().AllAwaitAsync(predicate).AsTask(); } /// /// Projects each element of an async-enumerable sequence into an async-enumerable /// sequence and merges the resulting async-enumerable sequences into one async-enumerable /// sequence /// /// The type of elements in the source sequence /// The type of elements in the projected inner sequences and the merged result sequence /// An async-enumerable sequence of elements to project /// An asynchronous selector function to apply to each element of the source sequence /// /// An async-enumerable sequence whose elements are the result of invoking the one-to-many /// transform function on each element of the source sequence and awaiting the result /// public static IAsyncEnumerable SelectManyAwait(this IEnumerable source, Func>> predicate) { async ValueTask> getAsyncEnumerable(TSource items) { var rez = await predicate(items); return rez.ToAsyncEnumerable(); } return source.ToAsyncEnumerable().SelectManyAwait(getAsyncEnumerable); } /// /// Projects each element of an async-enumerable sequence into an async-enumerable /// sequence and merges the resulting async-enumerable sequences into one async-enumerable /// sequence /// /// The type of elements in the source sequence /// The type of elements in the projected inner sequences and the merged result sequence /// An async-enumerable sequence of elements to project /// An asynchronous selector function to apply to each element of the source sequence /// /// An async-enumerable sequence whose elements are the result of invoking the one-to-many /// transform function on each element of the source sequence and awaiting the result /// public static IAsyncEnumerable SelectManyAwait(this IEnumerable source, Func>> predicate) { async ValueTask> getAsyncEnumerable(TSource items) { var rez = await predicate(items); return rez.ToAsyncEnumerable(); } return source.ToAsyncEnumerable().SelectManyAwait(getAsyncEnumerable); } /// /// Filters the elements of an async-enumerable sequence based on an asynchronous /// predicate /// /// /// An async-enumerable sequence whose elements to filter /// An asynchronous predicate to test each source element for a condition /// /// An async-enumerable sequence that contains elements from the input sequence that /// satisfy the condition /// public static IAsyncEnumerable WhereAwait(this IEnumerable source, Func> predicate) { return source.ToAsyncEnumerable().WhereAwait(predicate); } /// /// Determines whether any element in an async-enumerable sequence satisfies a condition /// /// The type of element in the sequence /// An async-enumerable sequence whose elements to apply the predicate to /// An asynchronous predicate to apply to each element of the source sequence /// /// A Task containing a value indicating whether any elements in the source /// sequence pass the test in the specified predicate /// /// A task that represents the asynchronous operation public static Task AnyAwaitAsync(this IEnumerable source, Func> predicate) { return source.ToAsyncEnumerable().AnyAwaitAsync(predicate).AsTask(); } /// /// Returns the only element of an async-enumerable sequence that satisfies the condition /// in the asynchronous predicate, or a default value if no such element exists, /// and reports an exception if there is more than one element in the async-enumerable /// sequence that matches the predicate /// /// The type of elements in the source sequence /// Source async-enumerable sequence /// An asynchronous predicate that will be applied to each element of the source sequence /// /// Task containing the only element in the async-enumerable sequence that satisfies /// the condition in the asynchronous predicate, or a default value if no such element /// exists /// /// A task that represents the asynchronous operation public static Task SingleOrDefaultAwaitAsync(this IEnumerable source, Func> predicate) { return source.ToAsyncEnumerable().SingleOrDefaultAwaitAsync(predicate).AsTask(); } /// /// Creates a list from an async-enumerable sequence /// /// The type of the elements in the source sequence /// The source async-enumerable sequence to get a list of elements for /// /// An async-enumerable sequence containing a single element with a list containing /// all the elements of the source sequence /// /// A task that represents the asynchronous operation public static Task> ToListAsync(this IEnumerable source) { return source.ToAsyncEnumerable().ToListAsync().AsTask(); } /// /// Sorts the elements of a sequence in descending order according to a key obtained /// by invoking a transform function on each element and awaiting the result /// /// The type of the elements of source /// The type of the key returned by keySelector /// An async-enumerable sequence of values to order /// An asynchronous function to extract a key from an element /// /// An ordered async-enumerable sequence whose elements are sorted in descending /// order according to a key /// public static IOrderedAsyncEnumerable OrderByDescendingAwait( this IEnumerable source, Func> keySelector) { return source.ToAsyncEnumerable().OrderByDescendingAwait(keySelector); } /// /// Groups the elements of an async-enumerable sequence and selects the resulting /// elements by using a specified function /// /// The type of the elements in the source sequence /// The type of the grouping key computed for each element in the source sequence /// The type of the elements within the groups computed for each element in the source sequence /// An async-enumerable sequence whose elements to group /// An asynchronous function to extract the key for each element /// An asynchronous function to map each source element to an element in an async-enumerable group /// /// A sequence of async-enumerable groups, each of which corresponds to a unique /// key value, containing all elements that share that same key value /// public static IAsyncEnumerable> GroupByAwait( this IEnumerable source, Func> keySelector, Func> elementSelector) { return source.ToAsyncEnumerable().GroupByAwait(keySelector, elementSelector); } /// /// Applies an accumulator function over an async-enumerable sequence, returning /// the result of the aggregation as a single element in the result sequence. The /// specified seed value is used as the initial accumulator value /// /// specified seed value is used as the initial accumulator value /// The type of the result of aggregation /// An async-enumerable sequence to aggregate over /// The initial accumulator value /// An asynchronous accumulator function to be invoked and awaited on each element /// A Task containing the final accumulator value public static ValueTask AggregateAwaitAsync( this IEnumerable source, TAccumulate seed, Func> accumulator) { return source.ToAsyncEnumerable().AggregateAwaitAsync(seed, accumulator); } /// /// Creates a dictionary from an async-enumerable sequence using the specified asynchronous /// key and element selector functions /// /// The type of the elements in the source sequence /// The type of the dictionary key computed for each element in the source sequence /// The type of the dictionary value computed for each element in the source sequence /// An async-enumerable sequence to create a dictionary for /// An asynchronous function to extract a key from each element /// An asynchronous transform function to produce a result element value from each element /// /// A Task containing a dictionary mapping unique key values onto the corresponding /// source sequence's element /// public static ValueTask> ToDictionaryAwaitAsync( this IEnumerable source, Func> keySelector, Func> elementSelector) where TKey : notnull { return source.ToAsyncEnumerable().ToDictionaryAwaitAsync(keySelector, elementSelector); } /// /// Groups the elements of an async-enumerable sequence according to a specified /// key selector function /// /// The type of the elements in the source sequence /// The type of the grouping key computed for each element in the source sequence /// An async-enumerable sequence whose elements to group /// An asynchronous function to extract the key for each element /// /// A sequence of async-enumerable groups, each of which corresponds to a unique /// key value, containing all elements that share that same key value /// public static IAsyncEnumerable> GroupByAwait(this IEnumerable source, Func> keySelector) { return source.ToAsyncEnumerable().GroupByAwait(keySelector); } /// /// Computes the sum of a sequence of System.Decimal values that are obtained by /// invoking a transform function on each element of the source sequence and awaiting /// the result /// /// The type of elements in the source sequence /// A sequence of values that are used to calculate a sum /// An asynchronous transform function to apply to each element /// A Task containing the sum of the values in the source sequence public static ValueTask SumAwaitAsync(this IEnumerable source, Func> selector) { return source.ToAsyncEnumerable().SumAwaitAsync(selector); } }