I have two lists, StockOld & StockNew. They are a custom type called 'Stock'. Stock has 5 properties but for my purposes if the two properties, isin & region are the same in both lists the objects should be treated as equal. What I want if to create a new list that selects all the objects that are equal in StockOld & StockNew. Hopefully the example below will help demonstrate what I mean.
StockOld StockNew Result I would like
ISIN Region ISIN Region ISIN Region
ABC1 UK DFG3 EU ABC1 UK
ERT4 US ABC1 UK LMN8 EU
LMN8 EU PLK2 EU
LMN8 EU
I have created a comparer class that implements IEqualityComparer, please see below.
public class ComparerISINRegion : IEqualityComparer<Stock>
{
public bool Equals(Stock x, Stock y)
{
return x.ISIN == y.ISIN && x.Region == y.Region;
}
public int GetHashCode(Stock obj)
{
int hash = 17;
hash = hash * 23 + (obj.ISIN ?? "").GetHashCode();
hash = hash * 23 + (obj.Region ?? "").GetHashCode();
return hash;
}
}