慕勒3428872
你不需要写任何代码。使用MoreLINQ批处理方法,它将源序列分批到大小相同的桶中(MoreLINQ可作为可以安装的NuGet包使用):int size = 10;var batches = sequence.Batch(size);其实施方式是:public static IEnumerable<IEnumerable<TSource>> Batch<TSource>(
this IEnumerable<TSource> source, int size){
TSource[] bucket = null;
var count = 0;
foreach (var item in source)
{
if (bucket == null)
bucket = new TSource[size];
bucket[count++] = item;
if (count != size)
continue;
yield return bucket;
bucket = null;
count = 0;
}
if (bucket != null && count > 0)
yield return bucket.Take(count);}