|
-
Jun 10th, 2013, 06:33 AM
#1
Thread Starter
Fanatic Member
[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
-
Jun 10th, 2013, 08:40 AM
#2
Thread Starter
Fanatic Member
Re: Combine Three List(Of string) to listview LINQ
Just noticed this will duplicate each item more then once.
-
Jun 10th, 2013, 09:25 AM
#3
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
-
Jun 10th, 2013, 09:35 AM
#4
Thread Starter
Fanatic Member
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
-
Jun 17th, 2013, 06:43 AM
#5
Thread Starter
Fanatic Member
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?
-
Jun 17th, 2013, 07:14 AM
#6
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.
-
Jun 17th, 2013, 08:06 AM
#7
Thread Starter
Fanatic Member
Re: Combine Three List(Of string) to listview LINQ
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|