VB.Net Generic List Get Unique Values By A Single Property

Posted on 2010-01-08
Last Modified: 2013-11-07
I have a Generic LIst I need to populate another Generic List with Only the Unique Objects bases Only on a single Property called Name.

I'm using .Net 2.0 - I can't use LINQ !   (Which would be easy...)

Any ideas of the best way... thanks

Question by:JElster
    LVL 15

    Accepted Solution

    I think you need distinct "Name" from Objects in MyList.
    For that, use a dictionary:

    Dim D as new Dictionary (of String,MyObjectType)(MyList.Count)
    For each e in MyList
       if D.Contains(e.Name) = False then D.Add(Name, e)
    Dim NewList as List(of String)(D.Keys) '        Dim NewList as List(of MyObjectType)(D.Values)
    LVL 11

    Expert Comment

    actually there is an easier way with the list but I don't know the VB syntax. I ll fire up VS2008 and let you know in a minute.
    LVL 11

    Assisted Solution

    ok I finally found out the syntax (I hate VB...):

    The idea is to use Linq's Distinct() method and provide it with a method to compare your objects (see code below).

    Another solution could be to do a group and then select one element (the first or last or whatever):
    Dim distinctList = fullList.GroupBy(Function(n) n.Name).Select(Function(o) o.First())

    Public Class NameCompare
            Implements System.Collections.Generic.IEqualityComparer(Of MyObject)
            Public Function Equals1(ByVal x As MyObject, ByVal y As MyObject) As Boolean Implements   System.Collections.Generic.IEqualityComparer(Of MyObject).Equals
                Return IIf(x.Name = y.Name, True, False)
            End Function
            Public Function GetHashCode1(ByVal x As MyObject) As Integer Implements System.Collections.Generic.IEqualityComparer(Of MyObject).GetHashCode
                Return x.Name.ToString.ToLower.GetHashCode()
            End Function
        End Class
    Dim fullList As List(Of MyObject)
    Dim distinctList = fullList.Distinct(New NameCompare())

    Open in new window

    LVL 11

    Expert Comment

    mmmmm never mind that ... I just realised in your question that you can't use Linq (I read the contrary).

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now