0

Im trying to find out a way to convert a 2d array of one type to another in a single line of code.

This is a personal learning experience rather than a need to do it in one line!!

Ive gotten so far as to convert it into IEnumerable<Tuple<ushort,ushort>>. Not sure where to go from here.

int[,] X = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };

var Result = (from e in X.OfType<int>() select e)
                .Select(S => (ushort)S)
                .Select((value, index) => new { Index = index, Value = value })
                      .GroupBy(x => x.Index / 2)
                      .Select(g => new ushort[,] { { g.ElementAt(0).Value, 
                                                     g.ElementAt(1).Value } });

Need to somehow convert the collection of Tuples into a ushort[,]

EDIT:

Just clarifying the question.

How do I convert a int 2d array into a ushort 2d array using a single line of code in linq?

EDIT:

Ive updated my code.

I now have it resulting in a IEnumerable collection of ushort[,].

I need to now find a way to concatonate all these into a single ushort[,]

CathalMF
  • 9,705
  • 6
  • 70
  • 106
  • I guess your question is how to convert `2d` array to array of `Tuple` right? – Hari Prasad Apr 14 '16 at 09:44
  • @HariPrasad, No the question is "how to convert a int 2d array into a ushort 2d array using a single line of code in linq" – CathalMF Apr 14 '16 at 09:46
  • 1
    Possible duplicate of [How can I convert a boxed two-dimensional array to a two-dimensional string array in one step?](http://stackoverflow.com/questions/6065695/how-can-i-convert-a-boxed-two-dimensional-array-to-a-two-dimensional-string-arra) – Timothée Bourguignon Apr 14 '16 at 09:51
  • 1
    Another duplicate which sounds more fitting this particular question: http://stackoverflow.com/questions/25658557/converting-multidimensional-array-elements-to-different-type – Jakub Szumiato Apr 14 '16 at 10:00

3 Answers3

3

The best I could come up with to keep the result two dimensional is this:

var input = new [,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
var output = new ushort[input.GetUpperBound(0) + 1, input.GetUpperBound(1) + 1];
Buffer.BlockCopy(input.Cast<int>().Select(x => (ushort)x).ToArray(), 0, output, 0, input.GetLength(0) * input.GetLength(1) * sizeof(ushort));
Dmitri Trofimov
  • 753
  • 3
  • 14
0

Using explicit cast to ushort we could do this, I'm leaving it to you to explore consequences in the conversion and addressing them.

int[,] X = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 } };
ushort[,] shortArray = new ushort[X.GetUpperBound(0)+1, X.GetUpperBound(1)+1];

for (int i = 0; i <= X.GetUpperBound(0); ++i) 
{
    for(int j=0;j<= X.GetUpperBound(1);j++)

        shortArray[i, j] = (ushort)X[i,j];         
}

In case you are interested on Jagged array instead of multidimensional array, use this.

var jagged =  X.Cast<int>()     
               .Select((x, i) => new { Index = i, Value = x })
               .GroupBy(x => x.Index / (X.GetUpperBound(1) +1))
               .Select(x => x.Select(s=> (ushort)s.Value).ToArray())
               .ToArray();

Working example

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
  • 1
    Yes i know its easy using loops. Im specifically trying to accomplish this using a single linq line. – CathalMF Apr 14 '16 at 10:14
  • Conversion is eay but multidimensional array creation is a problem, how about using jagged array (second approach)? – Hari Prasad Apr 14 '16 at 10:18
-1

How about:

var Result = X.OfType<int>().Select(s => new { Index = (s + 1) / 2, Value = s})
                            .GroupBy(g => g.Index)
                            .Select(s => s.Select(g => (ushort)g.Value).ToArray())
                            .ToArray();
shlatchz
  • 1,612
  • 1
  • 18
  • 40
  • 2
    Thought of that but the result is a ushort[][] and not a ushort [,] – CathalMF Apr 14 '16 at 10:13
  • 2
    You will need a helper function to convert the jagged array to a multi-dimensional array, as explained here: http://stackoverflow.com/questions/18896150/c-sharp-linq-return-a-multidimensional-array-from-linq – shlatchz Apr 14 '16 at 10:17