Click here to Skip to main content
16,015,827 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do I remove duplicate objects from a list?
For example:
C#
List(MyObject) list = new list

myObject.SomeAttribute = x;
I want to remove the duplicates by looking at each object's attribute.

A working code example would be appreciated
Posted
Updated 14-Jul-12 10:15am
v3
Comments
Zoltán Zörgő 14-Jul-12 15:31pm    
Ok, but let's say you have three objects, with the same SomeAttribute value, but all other attributes are different. Which one will remain of the three, and which two will be removed?
Sergey Alexandrovich Kryukov 14-Jul-12 23:12pm    
Please, stop re-posting.
--SA

1 solution

See following code:
C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace t1
{
    class MyClass
    {
        public int? property {get; set;}
        public int ID {get; set;}
    }

    class MyClassComparer : IEqualityComparer<MyClass>
    {
        public bool Equals(MyClass x, MyClass y)
        {
            if (Object.ReferenceEquals(x, y)) return true;
            if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
                return false;
            return x.property == y.property;
        }

        public int GetHashCode(MyClass myObject)
        {
            if (Object.ReferenceEquals(myObject, null)) return 0;

            int hashObjectProperty = myObject.property == null ? 0 : myObject.property.GetHashCode();

            return hashObjectProperty;
    }
    }

    class Program
    {
        static void Main()
        {
            List<MyClass> list = new List<MyClass>();

            Console.WriteLine("\nOriginal list:");

            foreach(int i in Enumerable.Range(1,10))
            {
                list.Add(new MyClass{ ID = i, property = (i & 1)});
                Console.WriteLine("{0} => {1}", i, i & 1);
            }

            Console.WriteLine("\nDistinct by property:");

            foreach(MyClass myObject in list.Distinct(new MyClassComparer()))
            {
                Console.WriteLine("{0} => {1}", myObject.ID, myObject.property);
            }

            Console.ReadKey();
        }

    }
}


As you will see if you run it, IEnumerble.Distinct will do the job for you with the help of a proper comparer, but it will leave the first occurrences. Thus, as I mentioned in my comment, you need to define the comparison means by implementing a proper IEqualityComparer[^] - this one could also help you: A Generic IEqualityComparer for Linq Distinct()[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 14-Jul-12 23:14pm    
My 5. Pay attention though: second re-post by OP. It's the best to remove extra post and answer only to the original question.
--SA
Zoltán Zörgő 15-Jul-12 3:20am    
I did not realize, that's another of this topic out there :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900