ExperimentalEnumerableBatchTSource, TBucket, TResult(IEnumerableTSource, Int32, ArrayPoolTSource, FuncICurrentBufferTSource, IEnumerableTBucket, FuncIEnumerableTBucket, TResult) Method

Batches the source sequence into sized buckets using an array pool to rent arrays to back each bucket and returns a sequence of elements projected from each bucket.

Definition

Namespace: MoreLinq.Experimental
Assembly: MoreLinq (in MoreLinq.dll) Version: 4.1.0+0e154ef592f33ce0f6f3d534a9eedee273f0ce72
C#
public static IEnumerable<TResult> Batch<TSource, TBucket, TResult>(
	this IEnumerable<TSource> source,
	int size,
	ArrayPool<TSource> pool,
	Func<ICurrentBuffer<TSource>, IEnumerable<TBucket>> bucketProjectionSelector,
	Func<IEnumerable<TBucket>, TResult> resultSelector
)

Parameters

source  IEnumerableTSource
The source sequence.
size  Int32
Size of buckets.
pool  ArrayPoolTSource
The pool used to rent the array for each bucket.
bucketProjectionSelector  FuncICurrentBufferTSource, IEnumerableTBucket
A function that returns a sequence projection to use for each bucket. It is called initially before iterating over source, but the resulting projection is evaluated for each bucket. This has the same effect as calling bucketProjectionSelector for each bucket, but allows initialization of the transformation to happen only once.
resultSelector  FuncIEnumerableTBucket, TResult
A function that projects a result from the input sequence produced over a bucket.

Type Parameters

TSource
Type of elements in source sequence.
TBucket
Type of elements in the sequence returned by bucketProjectionSelector.
TResult
Type of elements of the resulting sequence.

Return Value

IEnumerableTResult
A sequence whose elements are projected from each bucket (returned by resultSelector).

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerableTSource. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Remarks

This operator uses deferred execution and streams its results (however, each bucket is buffered).

Each bucket is backed by a rented array that may be at least size in length.

When more than one bucket is produced, all buckets except the last is guaranteed to have size elements. The last bucket may be smaller depending on the remaining elements in the source sequence.

Each bucket is pre-allocated to size elements. If size is set to a very large value, e.g. MaxValue to effectively disable batching by just hoping for a single bucket, then it can lead to memory exhaustion (OutOfMemoryException).

See Also