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.
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.
You don't need to write any code. Use MoreLINQ Batch method, which batches the source sequence into sized buckets (MoreLINQ is available as a NuGet package you can install):
Which is implemented as:
This is a fully lazy, low overhead, one-function implementation of Batch that doesn't do any accumulation. Based on (and fixes issues in) Nick Whaley's solution with help from EricRoller.
Iteration comes directly from the underlying IEnumerable, so elements must be enumerated in strict order, and accessed no more than once. If some elements aren't consumed in an inner loop, they are discarded (and trying to access them again via a saved iterator will throw
InvalidOperationException: Enumeration already finished.
).You can test a complete sample at .NET Fiddle.
Same approach as MoreLINQ, but using List instead of Array. I haven't done benchmarking, but readability matters more to some people:
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<TSource,Int32,TResult>
transform function. Like:and the usage would be:
OUTPUT:
I wrote a custom IEnumerable implementation that works without linq and guarantees a single enumeration over the data. It also accomplishes all this without requiring backing lists or arrays that cause memory explosions over large data sets.
Here are some basic tests:
The Extension Method to partition the data.
This is the implementing class