Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a list of objects declared in a class Toponyme implementing IEQuatable with four proprties City, CityGeoCode, County, State, Country.
I'm trying to remove the duplicates but without sucess.

What I have tried:

var liste = Liste.GroupBy(c => c.City + c.CityGeoCode + c.County + c.Region + c.Country).Select(c => c.First()).ToList();
and also

IEnumerable<toponyme> liste2 = Liste.Distinct().ToList();

Both without success.

Thanks for your help.
Posted
Updated 27-Aug-17 0:18am

1 solution

The way I'd do it is to use Distinct with a comparer:
public class MyClass
    {
    public string Name { get; set; }
    public string Other { get; set; }
    public MyClass(string a, string b)
        {
        Name = a; Other = b;
        }
    }
public class MyClassComp : IEqualityComparer<MyClass>
    {
    public bool Equals(MyClass x, MyClass y)
        {
        return x.Name == y.Name && x.Other == y.Other;
        }

    public int GetHashCode(MyClass obj)
        {
        if (Object.ReferenceEquals(obj, null)) return 0;
        int hashName = obj.Name == null ? 0 : obj.Name.GetHashCode();
        int hashOther = obj.Other == null ? 0 : obj.Other.GetHashCode();
        return hashName ^ hashOther;
        }
    }
I can then use that to do what you want:
List<MyClass> list = new List<MyClass>();
list.Add(new MyClass("A", "A"));
list.Add(new MyClass("A", "A"));
list.Add(new MyClass("A", "B"));
list.Add(new MyClass("A", "C"));
list.Add(new MyClass("B", "A"));
list.Add(new MyClass("B", "A"));
list.Add(new MyClass("B", "B"));
list.Add(new MyClass("B", "B"));
List<MyClass> newList = list.Distinct(new MyClassComp()).ToList();
 
Share this answer
 
Comments
Graeme_Grant 27-Aug-17 7:52am    
5'ed :)
BernardBouree 28-Aug-17 5:08am    
Thanks it works!
What about if I want to have the list of the duplicates?
OriginalGriff 28-Aug-17 5:28am    
That's where .Except comes in ... :laugh:
BernardBouree 28-Aug-17 13:07pm    
Well I don't know what is going on but it does not work any more.

Here is how I have implemented my comparison class

public class ToponymeGeoDbComp : IEqualityComparer<toponymegeodb>
{
public bool Equals(ToponymeGeoDb x, ToponymeGeoDb y)
{
return string.Equals(x.City.Trim(), y.City.Trim(), StringComparison.CurrentCultureIgnoreCase)
&& string.Equals(x.Country.Trim(), y.Country.Trim(), StringComparison.CurrentCultureIgnoreCase);
}

public int GetHashCode(ToponymeGeoDb obj)
{
if (ReferenceEquals(obj, null)) return 0;
var hashCodeCity = obj.City == null ? 0 : obj.City.GetHashCode();
var hashCodeCountry = obj.Country == null ? 0 : obj.Country.GetHashCode();
return hashCodeCity ^ hashCodeCountry;
}
}
And I'm calling it with
ToponymeGeoDb.ListeToponymesGeoDB = ToponymeGeoDb.ListeToponymesGeoDB.Distinct(new ToponymeGeoDbComp()).ToList();

Where ListeToponymesGeob is declared as static in my class ToponymeGeoDb.

When I try to trace I discover that the Equals method is never called .

Thnaks for the help.
OriginalGriff 28-Aug-17 14:05pm    
It does for me - copy'n'paste my code and you'll see - so you need to start by looking at the definition and content of ListeToponymesGeoDB immediately before your Distinct call is made.

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