0

I'm trying to add data to a multidimensional array but keep getting the error: "Cannot implicitly convert type 'Object[][]' to 'string[,]'". Dunno how to fix this. I found this on SO but their solution didn't help me: Datatable to Multidimensional Array

Any help would be greatly appreciated.

clDataTable = i_dbExec.ExecSelect(i_strQuery);
var tableEnumerable = clDataTable.AsEnumerable().Select(row => 
row.ItemArray).ToArray();
string[,] o_astrLocationCodes = tableEnumerable.ToArray(); <-- Error here
Wilock
  • 49
  • 6
  • 1
    Why don't you use `var o_astrLocationCodes` and iterate that? – hardkoded Sep 27 '17 at 21:42
  • 2
    1. `tableEnumerable` should already be an `Array` - you have `ToArray()` on the expression that initializes it. 2. Multidimensional arrays are not useful except in certain specific (normally mathematical) circumstances - why do you think it would be preferable to the `object[][]` `Array` you already have? 3. apparently the type of `.ItemArray` is `object[]` so it won't convert to `string[]` unless you convert each element. – NetMage Sep 27 '17 at 21:43
  • Op, expand a bit more on what you're trying to accomplish. There is likely a better approach to take. But, it is possible to do what you want, no matter how non-proper it seems. – Cory Sep 27 '17 at 22:04

2 Answers2

1

It's ugly, and I don't know why you would do it this way, but I don't judge.

string[][] stringArray = 
    clDataTable.AsEnumerable()
    .Select(
        row => row.ItemArray
        .Select(
            i=>i.ToString()
        ).ToArray()
    ).ToArray();
Cory
  • 1,794
  • 12
  • 21
0

Thank you @Cory and @NetMage for your answers. I thought I was constrained to providing an existing function (that I wasn't allowed to edit) with a multidimensional array, hence my question. But, I managed to find another workaround that didn't need the array. I'll mark Cory's answer as a solution since I could have used that in spite of his reservations... :)

Wilock
  • 49
  • 6