您无需编写任何代码。使用MoreLINQ
Batch方法,它将源序列分批处理成大小的存储桶(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).ToArray();}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)