I read in a few places that LINQ equals (in join syntax) calls IEquatable for the type it’s comparing, but I don’t see it happening in my join:
List<ILData> list1 = new List<ILData> { /* Initialized with items */ };
List<ILData> list2 = new List<ILData> { /* Initialized with items */ };
var joinItems = (
from d1 in list1
join d2 in list2
on d1 equals d2
where d1.SomeValue == "7"
select d1).Distinct().ToList<ILData>();
Assuming I provided an interface definition satisfying:
ILData : IEquatable<ILData>
requirements, why wouldn’t I be seeing ILData::Equals get called in this case?
It uses
EqualityComparer<T>.Default. Since your type implementsIEquatable<T>, the actual comparer used will be an internal class calledSystem.Collections.Generic.GenericEqualityComparer<T>, which will delegateGetHashCodeandEqualsrequests to yourobject.GetHashCodemethod andIEquatable<ILData>.Equalsmethods respectively (except fornullreferences, where it returns a zero hash-code / does a reference-equality check without calling your methods).There are a number of reasons that could explain your
Equalsmethod not being hit:object.Equalsrather thanIEquatable<ILData>.Equalsnullreferences (this is checked by the framework comparer, so it won’t enter yours).GetHashCode), so the join operation never needs to do a full equality check. One reason this might be happening is that you forgot to override this method and are therefore stuck with the default one fromobject(which is designed for reference equality and will be inconsistent with your definition of equality).