I am trying to generate a listbox of items that is a concatenation of two strings. I have created a class that implements IEqualityComparer and I want to make this list distinct.
private void PopulateFamily()
{
var source = _mfgOrdersData
.Select(o => new FamilySelector(o.ItemWrapper.ItemClass, o.ItemWrapper.Family))
.Distinct()
.OrderBy(f => f.CodeFamily)
.ToList();
FamilyFilterListBox.DataSource = source;
FamilyFilterListBox.ValueMember = "Family";
FamilyFilterListBox.DisplayMember = "CodeFamily";
}
class FamilySelector : IEqualityComparer<FamilySelector>
{
public FamilySelector(string code, string family)
{
Code = code;
Family = family;
}
public string Code { get; set; }
public string Family { get; set; }
public string CodeFamily { get { return string.Format("{0}\t{1}", Code, Family); } }
public bool Equals(FamilySelector x, FamilySelector y)
{
return x.Code == y.Code && x.Family == y.Family;
}
public int GetHashCode(FamilySelector obj)
{
return obj.Code.GetHashCode() ^ obj.Family.GetHashCode();
}
}
The problem is that I am getting the list non distinct. The same item appears multiple times. I presume that Equals() or GetHashCode() are not implemented correctly.