我无法从集合中删除重复项,我已经为 Employee 类实现了 IEqualityComparer 仍然没有得到输出
static void Main(string[] args)
{
List<Employe> Employeecollection = new List<Employe>();
Employeecollection.Add(new Employe("abc","def"));
Employeecollection.Add(new Employe("lmn","def"));
Employeecollection.Add(new Employe("abc", "def"));
IEnumerable<Employe> coll = Employeecollection.Distinct(new Employe());
foreach (Employe item in coll)
{
Console.WriteLine(item.fName + " " + item.lName );
}
}
下面是Employee类的实现,这里我实现了IEqualityComparer
class Employe : IEqualityComparer<Employe>
{
public string fName { get; set; }
public string lName { get; set; }
public Employe()
{
}
public Employe(string firstName, string LastName)
{
this.fName = firstName;
this.lName = LastName;
}
#region IEqualityComparer<pcf> Members
public bool Equals(Employe x, Employe y)
{
if (x.fName == y.fName && x.lName == y.lName)
{
return true;
}
return false;
}
public int GetHashCode(Employe obj)
{
return obj.GetHashCode();
}
#endregion
}