开发者

Removing duplicate items from a multicolumn listview

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 du开发者_如何学Goplicate 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:

Example of a duplicate

Maybe with this screenshot you can see what I am going for here. Thank you very much for being so patient with me!


Well, you'll need some method for determining whether two ListViewItem objects are duplicates.

Once that's in place, the implementation is fairly straightforward.

Let's say you want to consider two items to be the same if the text in the first column is the same (for example). Then you might write up a quick IEqualityComparer<ListViewItem> implementation such as:

class ListViewItemComparer : IEqualityComparer<ListViewItem>
{
    public bool Equals(ListViewItem x, ListViewItem y)
    {
        return x.Text == y.Text;
    }

    public int GetHashCode(ListViewItem obj)
    {
        return obj.Text.GetHashCode();
    }
}

Then you could remove duplicates like so:

static void RemoveDuplicateListViewItems(ListView listView)
{
    var uniqueItems = new HashSet<ListViewItem>(new ListViewItemComparer());

    for (int i = listView.Count - 1; i >= 0; --i)
    {
        // An item will only be added to the HashSet<ListViewItem> if an equivalent
        // item is not already contained within. So a return value of false indicates
        // a duplicate.
        if (!uniqueItems.Add(listView.Items[i]))
        {
            listView.Items.RemoveAt(i);
        }
    }
}

UPDATE: The above code removes the duplicates of any items that appear in the ListView more than once; that is, it leaves one instance of each. If the behavior you want is actually to remove all instances of any items that appear more than once, the approach is a little bit different.

Here's one way you could do it. First, define the following extension method:

public static bool CountAtLeast<T>(this IEnumerable<T> source, int minimumCount)
{
    int count = 0;
    foreach (T item in source)
    {
        if ((++count) >= minimumCount)
        {
            return true;
        }
    }

    return false;
}

Then, find duplicates like so:

static void RemoveDuplicateListViewItems(ListView listView)
{
    var duplicates = listView.Items.Cast<ListViewItem>()
        .GroupBy(item => item.Text)
        .Where(g => g.CountAtLeast(2))
        .SelectMany(g => g);

    foreach (ListViewItem duplicate in duplicates)
    {
        listView.Items.RemoveByKey(duplicate.Name);
    }
}

UPDATE 2: It sounds like you've been able to convert most of the above to VB.NET already. The line that is giving you trouble can be written as follows:

' Make sure you have Option Infer On. '
Dim duplicates = listView.Items.Cast(Of ListViewItem)() _
    .GroupBy(Function(item) item.Text) _
    .Where(Function(g) g.CountAtLeast(2)) _
    .SelectMany(Function(g) g)

Also, in case you have any trouble using the CountAtLeast method in the above way, you need to use the ExtensionAttribute class to write extension methods in VB.NET:

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜