Results 1 to 7 of 7

Thread: [RESOLVED] Combine Three List(Of string) to listview LINQ

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Resolved [RESOLVED] Combine Three List(Of string) to listview LINQ

    Hi so basically i have Three Lists that i wont to combine into One to add to a ListView.

    So ListViewItem 0 Text would be the first lists index 0, the next lists index 0 would be it's sub item, and finally the third lists index 0 would be another sub item.

    The next ListViewItem would be index 1 from the first lists. Just wondered if this is ok. It works.

    Code:
            Dim one As New List(Of String)({"OneZero", "OneOne", "OneTwo"})
            Dim two As New List(Of String)({"TwoZero", "TwoOne", "TwoTwo"})
            Dim three As New List(Of String)({"ThreeZero", "ThreeOne", "ThreeTwo"})
    
            Dim query = (
            From item1
            In one
            From item2
            In two
            From item3
            In three
            Select New ListViewItem(New String() {item1, item2, item3})).ToArray

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Re: Combine Three List(Of string) to listview LINQ

    Just noticed this will duplicate each item more then once.

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,376

    Re: Combine Three List(Of string) to listview LINQ

    Don't know to much about LINQ myself, but this is how I'd do it:
    Code:
            Dim one As New List(Of String)({"OneZero", "OneOne", "OneTwo"})
            Dim two As New List(Of String)({"TwoZero", "TwoOne", "TwoTwo"})
            Dim three As New List(Of String)({"ThreeZero", "ThreeOne", "ThreeTwo"})
    
            Dim col1, col2, col3 As New ColumnHeader
            ListView1.Columns.AddRange({col1, col2, col3})
    
            For i As Integer = 0 To one.Count - 1
                Dim new_item As New ListViewItem(one.Item(i))
                new_item.SubItems.AddRange({two.Item(i), three.Item(i)})
                ListView1.Items.Add(new_item)
            Next
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Re: Combine Three List(Of string) to listview LINQ

    Hi and thank you for the reply. I was using a for loop but trying to practice LINQ. I know LINQ is not always the answer but trying to learn more

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Re: Combine Three List(Of string) to listview LINQ

    Still struggling. I guess is that the only way by looping the first array by count?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Combine Three List(Of string) to listview LINQ

    Because you have the data in three different collections, you need to get an index from somewhere to get the three items to combine. You would normally do that using the loop counter in a For loop. You can use the Enumerable.Range method though, e.g.
    Code:
    ListView1.Items.AddRange(Enumerable.Range(0, list1.Count).
                                        Select(Function(i) New ListViewItem({list1(i), list2(i), list3(i)})).
                                        ToArray())
    Obviously that assumes that the lists are all the same length, or at least that list1 is not longer than the other two.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Re: Combine Three List(Of string) to listview LINQ

    Perfect as always

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