Getting a double[] row array of a double[,] rectangular array

后端 未结 6 1815
夕颜
夕颜 2021-02-13 13:43

Suppose you have an array like:

double[,] rectArray = new double[10,3];

Now you want the fouth row as a double[] array of 3 elements without do

6条回答
  •  被撕碎了的回忆
    2021-02-13 14:20

    LINQ to the rescue:

    var s = rectArray.Cast().Skip(9).Take(3).ToArray();
    

    Explanation: Casting a multi-dimensional array flattens it to a single-dimensional array. After that all we need to do is skip to the element we want (the 4th element in the 2-D array resolves to Skip(9)...) and take 3 elements from it).

提交回复
热议问题