Distinct () does not call equal methods

I implemented IEqualityComparer and IEquatable (both to be sure), but when I call the Distinct () method on the collection, it does not call the methods that come with it. Here is the code that I execute when I call the Distinct () function.

ObservableCollection<GigViewModel> distinctGigs = new ObservableCollection<GigViewModel>(Gigs.Distinct<GigViewModel>()); return distinctGigs; 

I want to return an ObservableCollection that does not contain any double objects found in the ObservableCollection 'Gigs' collection.

I implement such interfaces in the GigViewModel class:

 public class GigViewModel : INotifyPropertyChanged, IEqualityComparer<GigViewModel>, IEquatable<GigViewModel> { .... } 

And override the methods that come with such interfaces:

 public bool Equals(GigViewModel x, GigViewModel y) { if (x.Artiest.Naam == y.Artiest.Naam) { return true; } else { return false; } } public int GetHashCode(GigViewModel obj) { return obj.Artiest.Naam.GetHashCode(); } public bool Equals(GigViewModel other) { if (other.Artiest.Naam == this.Artiest.Naam) { return true; } else { return false; } } 

Thanks for all the help I get. So I created a separate class that implements IEqualityComparer and passed it to the instance in the disctinct method. But the methods are still not starting.

EqualityComparer:

 class GigViewModelComparer : IEqualityComparer<GigViewModel> { public bool Equals(GigViewModel x, GigViewModel y) { if (x.Artiest.Naam == y.Artiest.Naam) { return true; } else { return false; } } public int GetHashCode(GigViewModel obj) { return obj.Artiest.Naam.GetHashCode(); } } 

Call Distinct() :

  GigViewModelComparer comp = new GigViewModelComparer(); ObservableCollection<GigViewModel> distinctGigs = new ObservableCollection<GigViewModel>(Gigs.Distinct(comp)); return distinctGigs; 

EDIT2:

GetHashCode() Method GetHashCode() WIN! After introducing a new class. But the collection still contains duplicates. I have a "Gigs" list that contains an "Artiest" (or Artist) object. This artist has a Naam property, which is a string (Name).

+8
c # windows-phone silverlight windows-phone-8 observablecollection
source share
2 answers

So, you had an object that compares itself, implements both IEquatable and IEqualityComparer . It doesn’t make sense at all. IEquatable is a way of saying that an object can compare with something else. IEqualityComparer is a way of saying that it can compare two different things that you pass to each other. Usually you want to do one or the other, not both.

If you want to implement IEquatable , then the object must have not only the Equals method of the corresponding signature, but also override GetHashCode in order to have a reasonable implementation for this definition of equality. You did not do this . You created a GetHashCode method that takes an object as a parameter, but this is the overload used for IEqualityComparer . You must override the version with no parameters when using IEquatable (the one defined in Object ).

If you want to create a class that implements IEqualityComparer , you need to pass a comparative example to the Distinct method. Since you defined the object as your own correlator, you need to pass in some instance of this object as the second parameter. Of course, this does not make much sense in this sense; so it would be better if you go this route to pull the two methods that come with IEqualityComparer into a new type and instantiate this type for the Distinct method. If you actually passed an object with these definitions as a comparator, it will work fine.

+6
source share

Following the MSDN advice , you are best off creating a separate class for equality comparisons:

We recommend that you exit the EqualityComparer class instead of implementing the IEqualityComparer interface, because the EqualityComparer class tests for equality using IEquatable.Equals instead of the Object.Equals method. This corresponds to Contains, IndexOf, LastIndexOf and Remove dictionary class methods and other general collections.

So, create the GigViewModelComparer class, which comes from EqualityComparer and place the Equals and GetHashCode methods there.

Then pass an instance of this new comparator class when calling Gigs.Distinct(new GigViewModelComparer()) and it should work. Follow the example in the MSDN link above.

I have never seen anyone implement IEqualityComparer in the same class as the type of objects discussed in the collection. This is probably at least part of your problem.

+1
source share

All Articles