C# IEqualityComparer类型参数写法

时间:2022-04-22 16:10:22

最近在使用Union、Except时,由于默认的对比不太好使,所以需要自定义对比器,下面附上代码。

class MaterialListComparer : IEqualityComparer<WonderManager.Basal.Material.Domain.Material>
{
public bool Equals(WonderManager.Basal.Material.Domain.Material x, WonderManager.Basal.Material.Domain.Material y)
{
if (Object.ReferenceEquals(x, y)) return true;
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
return x.MaterialCode == y.MaterialCode;
} public int GetHashCode(WonderManager.Basal.Material.Domain.Material model)
{
if (Object.ReferenceEquals(model, null)) return 0; int hashProductName = model.MaterialCode == null ? 0 : model.MaterialCode.GetHashCode(); return hashProductName;
}
}