I need to create batches from a lazy enumerable with following requirements:
IEnumerable<IEnumerable<T>>
, excludes solution building arrays)Skip()
and Take()
)GroupBy
)The question is similar but more restrictive to followings:
Originally posted by @Nick_Whaley in Create batches in linq, but not the best response as the question was formulated differently:
Try this:
public static IEnumerable<IEnumerable<T>> Bucketize<T>(this IEnumerable<T> items, int bucketSize)
{
var enumerator = items.GetEnumerator();
while (enumerator.MoveNext())
yield return GetNextBucket(enumerator, bucketSize);
}
private static IEnumerable<T> GetNextBucket<T>(IEnumerator<T> enumerator, int maxItems)
{
int count = 0;
do
{
yield return enumerator.Current;
count++;
if (count == maxItems)
yield break;
} while (enumerator.MoveNext());
}
The trick is to pass the old-fashion enumerator between inner and outer enumeration, to enable continuation between two batches.