Create batches in linq

前端 未结 16 1847
傲寒
傲寒 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:42

        static IEnumerable> TakeBatch(IEnumerable ts,int batchSize)
        {
            return from @group in ts.Select((x, i) => new { x, i }).ToLookup(xi => xi.i / batchSize)
                   select @group.Select(xi => xi.x);
        }
    

提交回复
热议问题