I was looking at the source code of Batch
method and I have seen this:
// Select is necessary so bucket contents are streamed too
yield return resultSelector(bucket.Select(x => x));
There is a comment which I didn't quite understand. I have tested this method without using Select
and it worked well. But it seems there is something I'm missing.I can't think of any example where this would be necessary, So what's the actual purpose of using Select(x => x)
here ?
Here is the full source code for reference:
private static IEnumerable<TResult> BatchImpl<TSource, TResult>(
this IEnumerable<TSource> source,
int size,
Func<IEnumerable<TSource>, TResult> resultSelector)
{
TSource[] bucket = null;
var count = 0;
foreach (var item in source)
{
if (bucket == null)
bucket = new TSource[size];
bucket[count++] = item;
// The bucket is fully buffered before it's yielded
if (count != size)
continue;
// Select is necessary so bucket contents are streamed too
yield return resultSelector(bucket.Select(x => x));
bucket = null;
count = 0;
}
// Return the last bucket with all remaining elements
if (bucket != null && count > 0)
yield return resultSelector(bucket.Take(count));
}