Fatcatfats
Fatcatfats

Reputation: 53

How to compare the items in 2 different listviews

I'm trying to compare the items in one list view with items in another list view with a button press. I want to transfer all items from one view to the other with this press. But only the items that are not all ready in the second list view.

What I've tried so far:

ElseIf Not ListView8.Items.Count = 0 Then
    For Each item As ListViewItem In ListView8.Items
        For Each item1 As ListViewItem In ListView7.Items
            If Not item.Text.Equals(item1.Text) Then
                ListView7.Items.Remove(item1)
                ListView8.Items.Add(item1)
            End If
        Next
    Next
End If

When I execute this code it matches the first items in each list view then only one item changes then they don't match and that item is inserted even if it's all ready in the 2nd view.

Upvotes: 1

Views: 567

Answers (1)

user10216583
user10216583

Reputation:

You can do for example:

Dim newItems = From a In listView1.Items.OfType(Of ListViewItem)
                Where (
                    Aggregate b In listView2.Items.OfType(Of ListViewItem)
                        Where b.Text.ToLower.Equals(a.Text.ToLower)
                            Into Count()
                        ) = 0
                Select a

newItems.ToList.ForEach(Sub(a)
                            listView2.Items.Add(DirectCast(a.Clone, ListViewItem))
                            listView1.Items.Remove(a)
                        End Sub)

Upvotes: 2

Related Questions