Skip to content

Version 5.x.x SplitInBuckets (collections)

Pawel Gerr edited this page Mar 4, 2023 · 1 revision

Extension method for IEnumerable<T>, IReadOnlyCollection<T> and IReadOnlyList<T> for splitting a collection in multiple buckets with a maximal size of x.

var collection = new[] { 1, 2, 3, 4, 5, 6 };
var buckets = collection.SplitInBuckets(bucketSize: 5);

// enumerates twice:
// 1) bucket is [ 1, 2, 3, 4, 5 ]
// 2) bucket is [ 6 ]
foreach (IReadOnlyList<int> bucket in buckets)
{
    // do something
}