[RESOLVED] [2008] LINQ query on 2-Dimensional Arrays
What is the correct way to query a 2-D array with LINQ.
e.g.
What will be the LINQ equivalent of the following code:
vb.net Code:
Dim myArray(10, 1) As String
'--- Fill the entire array ---
myArray(0, 0) = "some text 0"
myArray(0, 1) = "value 0"
myArray(1, 0) = "some text 1"
myArray(1, 1) = "value 1"
myArray(2, 0) = "some text 2"
myArray(2, 1) = "value 2"
'....
Dim selection As New List(Of ListItem)
For i As Integer = 0 To myArray.GetLength(0) - 1
selection.Add(New ListItem(myArray(i, 0), myArray(i, 1)))
Next
I have tried things like these, but they don't work:
vb.net Code:
Dim selection = From item In myArray _
Select New ListItem With {.Text = item(0), .Value = item(1)}
Dim selection = From item In myArray _
Select New ListItem(item(0), item(1))
Pradeep :)
Re: [2008] LINQ query on 2-Dimensional Arrays
There wouldn't be an equivalent. You're trying to treat the array as though each "row" is an object. They aren't. A multi-dimensional array is a matrix where each element is a peer to all the others. There is no object that you can get from the array that contains two elements' values like you're trying to do there. No such object exists. All you can get from the array is individual elements. This is exactly why it is advisable to use 1D arrays or collections of objects with multiple properties rather than multi-dimensional arrays in the first place. There is no specific relationship between elements in the same "row' or "column" in a multi-dimensional array.
Re: [2008] LINQ query on 2-Dimensional Arrays
Thanks,
This is the exact answer I was looking for.
This confirms my doubts. :)