Convert 2 dimensional array
Solution 1:
If you mean a jagged array (T[][]
), SelectMany
is your friend. If, however, you mean a rectangular array (T[,]
), then you can just enumerate the date data via foreach
- or:
int[,] from = new int[,] {{1,2},{3,4},{5,6}};
int[] to = from.Cast<int>().ToArray();
Solution 2:
SelectMany is a projection operator, an extension method provided by the namespace System.Linq.
It performs a one to many element projection over a sequence, allowing you to "flatten" the resulting sequences into one.
You can use it in this way:
int[][] twoDimensional = new int[][] {
new int[] {1, 2},
new int[] {3, 4},
new int[] {5, 6}
};
int [] flattened = twoDimensional.SelectMany(x=>x).ToArray();