views:

33

answers:

1

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:

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!

+1  A: 

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
Dan Tao
I tried my best to convert it to VB.NET but I can't transpose this line correctly: var duplicates = listView.Items.Cast<ListViewItem>() .GroupBy(item => item.Text) .Where(g => g.CountAtLeast(2)) .SelectMany(g => g);Thank you for your help so far! I would put the rest of the translated code here but the character limit won't allow it.
RedHaze
@RedHaze: Sorry, missed the VB.NET tag! I will update the answer.
Dan Tao
I should be the one thanking you here! I am still having problems on that one line (I did have option infer on) but I am getting errors saying: "'.' or '!' can only appear inside a 'With' statement." I'll update the top with what I currently have so you know what I am working with.
RedHaze
@RedHaze: Well, I totally missed some necessary `_` characters; was that the issue?
Dan Tao
I tried adding the extra _ characters as well as having it all in one line but with little success. I get a different error when I do that: "Overload resolution failed because no 'Where' can be called with these arguments"
RedHaze
@RedGaze: It's definitely working for me. Care to update your question with the exact code that's causing the error?
Dan Tao
I added a link to an image up there so you can see where I'm receiving errors during compiling. Thank you for your patience!
RedHaze
@RedHaze: The image you linked to shows the errors resulting from leaving out the `_` line endings! Try adding those back in and then include a link to the other error you said you were getting ("Overload resolution failed...").
Dan Tao
I'm sorry about that. Here it is: http://imgur.com/S1Ea1.png
RedHaze
@RedHaze: Did you define the `CountAtLeast` method the way I did in my answer, in a module with the `Extension` attribute? It seems the VB.NET compiler in your screenshot is not aware of that method. Alternatively, you could make it a simple shared function and call it like this: `Function(g) CountAtLeast(g, 2)`
Dan Tao
That was exactly it. I got the code to compile and called the function inside my statement using: RemoveDuplicateListViewItems(ListView1) but nothing happened. I must be doing something really stupid somewhere in this code. I updated my post up there to show an image of the issue. Thank you so much!
RedHaze
@RedHaze: I think I can guess the problem. Do your `ListViewItem` objects not have their `Name` property assigned? It's no biggie: just use `Remove(duplicate)` or `RemoveAt(duplicate.Index)` instead of `RemoveByKey(duplicate.Name)`.
Dan Tao