Create batches in linq

前端 未结 16 1785
傲寒
傲寒 2020-11-22 02:50

Can someone suggest a way to create batches of a certain size in linq?

Ideally I want to be able to perform operations in chunks of some configurable amount.

16条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 03:27

    I know everybody used complex systems to do this work, and I really don't get it why. Take and skip will allow all those operations using the common select with Func transform function. Like:

    public IEnumerable> Buffer(IEnumerable source, int size)=>
        source.Select((item, index) => source.Skip(size * index).Take(size)).TakeWhile(bucket => bucket.Any());
    

提交回复
热议问题