Question Answered
Thank you Dan! Your code worked perfectly and you saved my life today! Many internets to you good sir.
Original
I was generously guided by the community to use LINQ to find duplicates on my listboxes the last time around. However, I am now in a tough spot because I need to find and remove duplicates from a multicolumn list view. I tried using LINQ but it says that the listview object is not "queryable". Is there a way for me to find and remove duplicates using only one column of the listview?
Thanks
UPDATE
Private Shared Sub RemoveDuplicateListViewItems(ByVal listView As ListView)
Dim duplicates = listView.Items.Cast(Of ListViewItem)() _
.GroupBy(Function(item) item.Text)
.Where(Function(g) g.CountAtLeast(2))
.SelectMany(Function(g) g)
For Each duplicate As ListViewItem In duplicates
listView.Items.RemoveByKey(duplicate.Name)
Next
End Sub
This is what I have so far thanks to Dan. Still getting errors on the "Dim duplicates" line.
UPDATE 2 Here is the code for the Module and the Function inside the form:
Imports System.Runtime.CompilerServices
Module CountAtLeastExtension
<Extension()> _
Public Function CountAtLeast(Of T)(ByVal source As IEnumerable(Of T), ByVal minimumCount As Integer) As Boolean
Dim count = 0
For Each item In source
count += 1
If count >= minimumCount Then
Return True
End If
Next
Return False
End Function
End Module
Private Shared Sub RemoveDuplicateListViewItems(ByVal listView As ListView)
Dim duplicates = listView.Items.Cast(Of ListViewItem)() _
.GroupBy(Function(item) item.Text) _
.Where(Function(g) g.CountAtLeast(2)) _
.SelectMany(Function(g) g)
For Each duplicate As ListViewItem In duplicates
listView.Items.RemoveByKey(duplicate.Name)
Next
End Sub
The code now runs fine when I call it. But it does not remove the duplicates:
Maybe with this screenshot you can see what I am going for here. Thank you very much for being so patient with me!