在Linq中创建批处理有人能建议在Linq中创建一定大小的批次吗?理想情况下,我希望能够在一定数量的块中执行操作。
3 回答
慕勒3428872
TA贡献1848条经验 获得超6个赞
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);}
largeQ
TA贡献2039条经验 获得超7个赞
sequence
IEnumerable<T>
while (sequence.Any()){ var batch = sequence.Take(10); sequence = sequence.Skip(10); // do whatever you need to do with each batch here}
- 3 回答
- 0 关注
- 519 浏览
添加回答
举报
0/150
提交
取消