Splitting an array using LINQ

前端 未结 7 1168
你的背包
你的背包 2020-12-10 15:02

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

7条回答
  •  悲哀的现实
    2020-12-10 15:58

    The sample below will split an array into groups of 4 items each.

    int[] items = Enumerable.Range(1, 20).ToArray(); // Generate a test array to split
    int[][] groupedItems = items
                             .Select((item, index) => index % 4 == 0 ? items.Skip(index).Take(4).ToArray() : null)
                             .Where(group => group != null)
                             .ToArray();
    

提交回复
热议问题