I have a collection uni-dimensional like this:
[1,2,4,5.....n]
I would like to convert that collection in a bi-dimensional collection like this:
[[1,2,3],
[4,5,6],
...]
Basically I want to group or split if you want, the array in groups of 'n' members
I can do it with a foreach
statement, but I am currently learning LINQ so instead of iterating through all elements and create a new array manually I would like to use the LINQ features (if applicable)
Is there any LINQ function to help me to accomplish this??
I was thinking in the GroupBy
or SelectMany
I do not know if they will help me though but they might
Any help will be truly appreciate it =) :**
It's not a pure LINQ but it's intended to be used with it:
It can be used from your code as:
You want
Take()
andSkip()
. These methods will let you split anIEnumerable
. Then you can useConcat()
to slap them together again.It's as simple as:
The sample below will split an array into groups of 4 items each.
You can group by the index divided by the batch size, like this:
Use MoreLinq.Batch
Example