XK8ER
asked on
vb.net - remove dups from listview
hello,
I am using this code to remove dups from listview and its working perfectly fine for small amounts like 100 or 200 items..
today I got a file that needs to be checked for dups but its 12,000 lines and it gets frozen..
how can I speed up the process and unfreeze?
I am using this code to remove dups from listview and its working perfectly fine for small amounts like 100 or 200 items..
today I got a file that needs to be checked for dups but its 12,000 lines and it gets frozen..
how can I speed up the process and unfreeze?
Public Sub RemoveDuplicates(ByVal lstView As ListView)
Try
Dim itemI, itemJ As ListViewItem
lstView.BeginUpdate()
For i As Integer = lstView.Items.Count - 1 To 0 Step -1
itemI = lstView.Items(i)
For z As Integer = i + 1 To lstView.Items.Count - 1 Step 1
itemJ = lstView.Items(z)
If itemI.Text = itemJ.Text Then
lstView.Items.Remove(itemJ)
Exit For
End If
Next z
Next (i)
lstView.EndUpdate()
Catch ex As Exception
End Try
End Sub
to distinct listview with 12000 items in the way i've described it takes less than 200 ms.
ASKER
can you please provide a vb.net answer?
Class DistinctListViewItemComparer
Implements IEqualityComparer(Of ListViewItem)
Public Overloads Function Equals(x As ListViewItem, y As ListViewItem) As Boolean
Return x.Text = y.Text
End Function
Public Overloads Function GetHashCode(obj As ListViewItem) As Integer
Return obj.Text.GetHashCode()
End Function
End Class
usage:
'get the distinct listview items
Dim items = listView1.Items.Cast(Of ListViewItem)().Distinct(New DistinctListViewItemComparer()).ToArray()
'clear the listview
listView1.Items.Clear()
'add the distinct listview items to the listview
listView1.Items.AddRange(items)
ASKER
this line
>>Implements IEqualityComparer(Of ListViewItem)
Error 1 Class 'DistinctListViewItemCompa rer' must implement 'Function Equals(x As Windows.Forms.ListViewItem , y As Windows.Forms.ListViewItem ) As Boolean' for interface 'System.Collections.Generi c.IEqualit yComparer( Of System.Windows.Forms.ListV iewItem)'.
>>Implements IEqualityComparer(Of ListViewItem)
Error 1 Class 'DistinctListViewItemCompa
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if u have list of string u can use Distinct() of linq:
var noDupsList = org_list.Distinct();
obviously u can do the same on the listview control but u need first a custom equality comparer:
Open in new window
then u need to run this code:
//get the distinct listview items
var items = listView1.Items.Cast<ListV
//clear the listview
listView1.Items.Clear();
//add the distinct listview items to the listview
listView1.Items.AddRange(i