0

Possible Duplicate:
Split List into Sublists with LINQ
Split a collection into n parts with LINQ?

I have an array like this:

[1,2,4,5.....n]

I would like to convert it in sub arrays like this:

[
   [1,2,3],
   [4,5,6],
   ...
]

Basically I want to group the array in groups of n members

Is there any LINQ function to help me to accomplish this??

I was thinking in the GroupBy or SelectMany but I have not figured out how to do it

Note, I already did this using a foreach statement, but I would like to do it using LINQ

Community
  • 1
  • 1
Jupaol
  • 21,107
  • 8
  • 68
  • 100

1 Answers1

0
        int[] ar = new int[] {1,2,3,4,5,6};
        var gr = ar
            .Select( (e, i) => new {e, p=i/3})
            .GroupBy( e => e.p )
            .Select( g => g
                .Select(e => e.e)
                .ToArray()
            )
            .ToArray();
Andrey
  • 59,039
  • 12
  • 119
  • 163