Splitting an array using LINQ

前端 未结 7 1155
你的背包
你的背包 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:50

    I based my solution of Jeremy Holovacs's answer and used Take() and Skip() to create subarrays.

            const int batchSize = 3;
            int[] array = new int[] { 1,2,4,5.....n};
    
            var subArrays = from index in Enumerable.Range(0, array.Length / batchSize + 1)
                          select array.Skip(index * batchSize).Take(batchSize);
    

提交回复
热议问题