I have two lists see below…..result is coming back as empty
List<Pay>olist = new List<Pay>();
List<Pay> nlist = new List<Pay>();
Pay oldpay = new Pay()
{
EventId = 1,
Number = 123,
Amount = 1
};
olist.Add(oldpay);
Pay newpay = new Pay ()
{
EventId = 1,
Number = 123,
Amount = 100
};
nlist.Add(newpay);
var Result = nlist.Intersect(olist);
any clue why?
You need to override the
EqualsandGetHashCodemethods in yourPayclass, otherwiseIntersectdoesn’t know when 2 instances are considered equal. How could it guess that it is theEventIdthat determines equality?oldPayandnewPayare different instances, so by default they’re not considered equal.You can override the methods in
Paylike this:Another option is to implement an
IEqualityComparer<Pay>and pass it as a parameter toIntersect: