Convert multidimensional array to jagged array in C#

前端 未结 3 1910
花落未央
花落未央 2020-12-17 18:10

I have a C# WCF webservice which is called by two VB 6 project. The target VB project is sending to the client VB project a multidimensional array.

I want to convert

3条回答
  •  抹茶落季
    2020-12-17 18:32

    This worked for me and did not require any looping. It took a object[85000,26] and converted it to object[85000][26] in a little over a second.

    object[,] obj2D = ...
    
    // Take my 2D array and cast it as a 1D array
    object[] obj1D = ((object[,]) obj2D).Cast().ToArray();
    
    // using linq, chunk the 1D array back into a jagged array
    Int32 j = 0;
    object[][] jagged = obj1D.GroupBy(x => j++ / obj2D.GetLength(1)).Select(y => y.ToArray()).ToArray();
    
        

    提交回复
    热议问题