Method Batch
View SourceBatch<TSource, TResult>(IEnumerable<TSource>, int, ReadOnlySpanFunc<TSource, TResult>)
Split the elements of a sequence into chunks of size at most size
.
Declaration
public static IEnumerable<TResult> Batch<TSource, TResult>(this IEnumerable<TSource> source, int size, SuperEnumerable.ReadOnlySpanFunc<TSource, TResult> resultSelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | An IEnumerable<T> whose elements to chunk. |
int | size | The maximum size of each chunk. |
SuperEnumerable.ReadOnlySpanFunc<TSource, TResult> | resultSelector | A transform function to apply to each chunk. |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | A sequence of elements returned by |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
TResult | The type of the value return by |
Remarks
A chunk can contain fewer elements than size
, specifically the final chunk of source
.
In this overload of Batch
, a single array of length size
is allocated as a buffer
for all subsequences.
This operator uses deferred execution and streams its results.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
Batch<TSource, TResult>(IEnumerable<TSource>, TSource[], ReadOnlySpanFunc<TSource, TResult>)
Split the elements of a sequence into chunks of size at most
.array
.Length
Declaration
public static IEnumerable<TResult> Batch<TSource, TResult>(this IEnumerable<TSource> source, TSource[] array, SuperEnumerable.ReadOnlySpanFunc<TSource, TResult> resultSelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | An IEnumerable<T> whose elements to chunk. |
TSource[] | array | The array to use as a buffer for each chunk |
SuperEnumerable.ReadOnlySpanFunc<TSource, TResult> | resultSelector | A transform function to apply to each chunk. |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | A sequence of elements returned by |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
TResult | The type of the value return by |
Remarks
A chunk can contain fewer elements than
, specifically the final chunk
of array
.Lengthsource
.
In this overload of Batch
, array
is used as a common buffer for all subsequences.
This operator uses deferred execution and streams its results.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Batch<TSource, TResult>(IEnumerable<TSource>, TSource[], int, ReadOnlySpanFunc<TSource, TResult>)
Split the elements of a sequence into chunks of size at most size
.
Declaration
public static IEnumerable<TResult> Batch<TSource, TResult>(this IEnumerable<TSource> source, TSource[] array, int size, SuperEnumerable.ReadOnlySpanFunc<TSource, TResult> resultSelector)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | An IEnumerable<T> whose elements to chunk. |
TSource[] | array | The array to use as a buffer for each chunk |
int | size | The maximum size of each chunk. |
SuperEnumerable.ReadOnlySpanFunc<TSource, TResult> | resultSelector | A transform function to apply to each chunk. |
Returns
Type | Description |
---|---|
IEnumerable<TResult> | A sequence of elements returned by |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
TResult | The type of the value return by |
Remarks
A chunk can contain fewer elements than size
, specifically the final chunk of source
.
In this overload of Batch
, array
is used as a common buffer for all
subsequences. This overload is provided to ease usage of common buffers, such as those rented from ArrayPool<T>, which may return an array larger than requested.
This operator uses deferred execution and streams its results.
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Batch<TSource>(IEnumerable<TSource>, int)
Split the elements of a sequence into chunks of size at most size
.
Declaration
public static IEnumerable<IList<TSource>> Batch<TSource>(this IEnumerable<TSource> source, int size)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TSource> | source | An IEnumerable<T> whose elements to chunk. |
int | size | The maximum size of each chunk. |
Returns
Type | Description |
---|---|
IEnumerable<IList<TSource>> | An IEnumerable<T> that contains the elements the input sequence split into chunks of size size. |
Type Parameters
Name | Description |
---|---|
TSource | The type of the elements of |
Remarks
A chunk can contain fewer elements than size
, specifically the final buffer of source
.
A separate array is allocated for each returned chunk. Other overloads of Batch
are available which
do not require additional array allocations for each chunk.
Returned subsequences are buffered, but the overall operation is streamed.
Examples
The following code example demonstrates how to break a sequence into chunks of a specific size, using Batch
.
var sequence = Enumerable.Range(1, 10);
// Break the sequence of numbers into three chunks of 3 and one chunk of 1
var result = sequence.Batch(3);
Console.WriteLine(
"[" +
string.Join(
", ",
result.Select(c => "[" + string.Join(", ", c) + "]")) +
"]");
// This code produces the following output:
// [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|