Class IAsyncEnumerableExtensions
- Namespace
- Wolfgang.Extensions.IAsyncEnumerable
- Assembly
- Wolfgang.Extensions.IAsyncEnumerable.dll
A collection of extension methods for IAsyncEnumerable{T}.
public static class IAsyncEnumerableExtensions
- Inheritance
-
IAsyncEnumerableExtensions
- Inherited Members
Methods
ChunkAsync<T>(IAsyncEnumerable<T>, int, CancellationToken)
Splits an IAsyncEnumerable{T} into chunks of a specified maximum size.
public static IAsyncEnumerable<ICollection<T>> ChunkAsync<T>(this IAsyncEnumerable<T> source, int maxChunkSize, CancellationToken token = default)
Parameters
sourceIAsyncEnumerable<T>The source IAsyncEnumerable{T} to chunk.
maxChunkSizeintThe maximum size of each chunk.
tokenCancellationTokenA cancellation token to cancel the operation.
Returns
- IAsyncEnumerable<ICollection<T>>
An IAsyncEnumerable{ICollection{T}} representing the chunks.
Type Parameters
TThe type of elements in the IAsyncEnumerable{T}.
Exceptions
- ArgumentNullException
Thrown when
sourceis null.- ArgumentOutOfRangeException
Thrown when
maxChunkSizeis less than one.
DoAsync<T>(IAsyncEnumerable<T>, Action<T>, CancellationToken)
Executes a synchronous side-effect action on each element of an IAsyncEnumerable{T} without transforming the elements. The original items are yielded unchanged.
public static IAsyncEnumerable<T> DoAsync<T>(this IAsyncEnumerable<T> source, Action<T> action, CancellationToken token = default)
Parameters
sourceIAsyncEnumerable<T>The source IAsyncEnumerable{T}.
actionAction<T>The synchronous action to execute on each element.
tokenCancellationTokenA cancellation token to cancel the operation.
Returns
- IAsyncEnumerable<T>
An IAsyncEnumerable{T} that yields the original elements after executing the action.
Type Parameters
TThe type of elements in the IAsyncEnumerable{T}.
Examples
await foreach (var item in source.DoAsync(x => Console.WriteLine($"Processing: {x}")))
{
// item is unchanged
}
Exceptions
- ArgumentNullException
Thrown when
sourceoractionis null.
DoAsync<T>(IAsyncEnumerable<T>, Func<T, Task>, CancellationToken)
Executes an asynchronous side-effect action on each element of an IAsyncEnumerable{T} without transforming the elements. The original items are yielded unchanged.
public static IAsyncEnumerable<T> DoAsync<T>(this IAsyncEnumerable<T> source, Func<T, Task> action, CancellationToken token = default)
Parameters
sourceIAsyncEnumerable<T>The source IAsyncEnumerable{T}.
actionFunc<T, Task>The asynchronous action to execute on each element.
tokenCancellationTokenA cancellation token to cancel the operation.
Returns
- IAsyncEnumerable<T>
An IAsyncEnumerable{T} that yields the original elements after executing the action.
Type Parameters
TThe type of elements in the IAsyncEnumerable{T}.
Examples
await foreach (var item in source.DoAsync(async x => await logger.LogAsync($"Processing: {x}")))
{
// item is unchanged
}
Exceptions
- ArgumentNullException
Thrown when
sourceoractionis null.
ForEachAsync<T>(IAsyncEnumerable<T>, Action<T>, CancellationToken)
Executes a synchronous action on each element of an IAsyncEnumerable{T}, consuming the sequence. This is a terminal operation.
public static Task ForEachAsync<T>(this IAsyncEnumerable<T> source, Action<T> action, CancellationToken token = default)
Parameters
sourceIAsyncEnumerable<T>The source IAsyncEnumerable{T}.
actionAction<T>The synchronous action to execute on each element.
tokenCancellationTokenA cancellation token to cancel the operation.
Returns
Type Parameters
TThe type of elements in the IAsyncEnumerable{T}.
Examples
await source.ForEachAsync(x => Console.WriteLine($"Processing: {x}"));
Exceptions
- ArgumentNullException
Thrown when
sourceoractionis null.
ForEachAsync<T>(IAsyncEnumerable<T>, Func<T, Task>, CancellationToken)
Executes an asynchronous action on each element of an IAsyncEnumerable{T}, consuming the sequence. This is a terminal operation.
public static Task ForEachAsync<T>(this IAsyncEnumerable<T> source, Func<T, Task> action, CancellationToken token = default)
Parameters
sourceIAsyncEnumerable<T>The source IAsyncEnumerable{T}.
actionFunc<T, Task>The asynchronous action to execute on each element.
tokenCancellationTokenA cancellation token to cancel the operation.
Returns
Type Parameters
TThe type of elements in the IAsyncEnumerable{T}.
Examples
await source.ForEachAsync(async x => await logger.LogAsync($"Processing: {x}"));
Exceptions
- ArgumentNullException
Thrown when
sourceoractionis null.
IsEmptyAsync<T>(IAsyncEnumerable<T>, CancellationToken)
Asynchronously determines whether a sequence contains no elements.
public static Task<bool> IsEmptyAsync<T>(this IAsyncEnumerable<T> source, CancellationToken token = default)
Parameters
sourceIAsyncEnumerable<T>The IAsyncEnumerable{T} to check.
tokenCancellationTokenA cancellation token to cancel the operation.
Returns
Type Parameters
TThe type of elements in the IAsyncEnumerable{T}.
Examples
if (await source.IsEmptyAsync())
{
Console.WriteLine("No items found.");
}
Exceptions
- ArgumentNullException
Thrown when
sourceis null.
IsNullOrEmptyAsync<T>(IAsyncEnumerable<T>?, CancellationToken)
Asynchronously determines whether a sequence is null or contains no elements.
public static Task<bool> IsNullOrEmptyAsync<T>(this IAsyncEnumerable<T>? source, CancellationToken token = default)
Parameters
sourceIAsyncEnumerable<T>The IAsyncEnumerable{T} to check.
tokenCancellationTokenA cancellation token to cancel the operation.
Returns
Type Parameters
TThe type of elements in the IAsyncEnumerable{T}.
Examples
if (await source.IsNullOrEmptyAsync())
{
Console.WriteLine("No items found.");
}
NoneAsync<T>(IAsyncEnumerable<T>, Func<T, bool>, CancellationToken)
Asynchronously determines whether no element of a sequence satisfies a condition.
public static Task<bool> NoneAsync<T>(this IAsyncEnumerable<T> source, Func<T, bool> predicate, CancellationToken token = default)
Parameters
sourceIAsyncEnumerable<T>The IAsyncEnumerable{T} whose elements to apply the predicate to.
predicateFunc<T, bool>A function to test each element for a condition.
tokenCancellationTokenA cancellation token to cancel the operation.
Returns
- Task<bool>
true if no elements in the source sequence pass the test in the specified predicate; otherwise, false.
Type Parameters
TThe type of elements in the IAsyncEnumerable{T}.
Examples
if (await source.NoneAsync(x => x > 100))
{
Console.WriteLine("No items greater than 100.");
}
Exceptions
- ArgumentNullException
Thrown when
sourceorpredicateis null.
NoneAsync<T>(IAsyncEnumerable<T>, CancellationToken)
Asynchronously determines whether a sequence contains no elements.
public static Task<bool> NoneAsync<T>(this IAsyncEnumerable<T> source, CancellationToken token = default)
Parameters
sourceIAsyncEnumerable<T>The IAsyncEnumerable{T} to check.
tokenCancellationTokenA cancellation token to cancel the operation.
Returns
Type Parameters
TThe type of elements in the IAsyncEnumerable{T}.
Examples
if (await source.NoneAsync())
{
Console.WriteLine("No items found.");
}
Exceptions
- ArgumentNullException
Thrown when
sourceis null.