Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1044055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:45:56+00:00 2026-05-16T15:45:56+00:00

Question Answered Thank you Dan! Your code worked perfectly and you saved my life

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:45:56+00:00Added an answer on May 16, 2026 at 3:45 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

question answered. Thank you all for your willingness to help and offer your aid.
Today I got this question for which I think I answered very bad. I
Firstly, I'd like to thank those who answered my previous question ages ago. Currently
You might think that this question is already answered. However, I couldn't find the
After having my last question answered , I have never see the preventDefault(); function
I had a question answered which raised another one, why following does not work?
I saw another similar question answered here - Velocity editor plugin for Eclipse? .
Ok, I got the first part of my question answered, so here's the second
I'm losing my mind on this one. My last question answered a syntax issue,
An earlier question was answered about getting the OrderID and the number of associated

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.