Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Something like this should work for 3->2:</p> <pre class="lang-cs prettyprint-override"><code>static T[][] Collapse&lt;T&gt;(T[][][] array, int y) { return Enumerable.Range(0, array.Length).Select(x =&gt; Enumerable.Range(0, array[x][y].Length).Select(z =&gt; array[x][y][z] ).ToArray() ).ToArray(); } </code></pre> <p>It should be fairly obvious how to extend this pattern to higher dimensional problems.</p> <p><em><strong>Edit:</strong></em></p> <p>This extends to multidimensional arrays in the expected manner. If you're interested in returning a multidimensional array (as opposed to a jagged array), LINQ probably isn't the tool that you want to use to solve this problem as it provides no obvious or intuitive methods for handling multidimensional array creation.</p> <p>If you really want the multidimensional array as output, I've included a sample "deburr" implementation that should collapse it back to a multidimensional (non-jagged) array.</p> <pre class="lang-cs prettyprint-override"><code>static T[][] Collapse&lt;T&gt;(T[,,] array, int y) { return Enumerable.Range(0, array.GetLength(0)).Select(x =&gt; Enumerable.Range(0, array.GetLength(2)).Select(z =&gt; array[x,y,z] ).ToArray() ).ToArray(); } static T[,] Deburr&lt;T&gt;(T[][] jagged) { T[,] mArray = new T[jagged.Length, jagged.Max(array =&gt; array.Length)]; foreach (int row in Enumerable.Range(0, jagged.Length)) foreach (int col in Enumerable.Range(0, jagged[row].Length)) mArray[row, col] = jagged[row][col]; return mArray; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload