Splitting an array using LINQ

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

    Use MoreLinq.Batch

     var result = inputArray.Batch(n); // n -> batch size
    

    Example

        var inputs = Enumerable.Range(1,10);
    
        var output = inputs.Batch(3);
    
    
        var outputAsArray = inputs.Batch(3).Select(x=>x.ToArray()).ToArray(); //If require as array
    

提交回复
热议问题