Results 1 to 3 of 3

Thread: [RESOLVED] [2008] LINQ query on 2-Dimensional Arrays

  1. #1

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Resolved [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:
    1. Dim myArray(10, 1) As String
    2. '--- Fill the entire array ---
    3. myArray(0, 0) = "some text 0"
    4. myArray(0, 1) = "value 0"
    5. myArray(1, 0) = "some text 1"
    6. myArray(1, 1) = "value 1"
    7. myArray(2, 0) = "some text 2"
    8. myArray(2, 1) = "value 2"
    9. '....
    10.  
    11. Dim selection As New List(Of ListItem)
    12. For i As Integer = 0 To myArray.GetLength(0) - 1
    13.     selection.Add(New ListItem(myArray(i, 0), myArray(i, 1)))
    14. Next

    I have tried things like these, but they don't work:
    vb.net Code:
    1. Dim selection = From item In myArray _
    2.                 Select New ListItem With {.Text = item(0), .Value = item(1)}
    3.  
    4. Dim selection = From item In myArray _
    5.                 Select New ListItem(item(0), item(1))

    Pradeep
    Last edited by Pradeep1210; Apr 10th, 2008 at 01:44 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    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.

  3. #3

    Thread Starter
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [2008] LINQ query on 2-Dimensional Arrays

    Thanks,
    This is the exact answer I was looking for.
    This confirms my doubts.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width