我在同一记录中有两次列出的对象列表。但是,当我应用与众不同的方法时,它不会删除,因为它区分大小写。
我尝试了三种方式:
static void Main(string[] args)
{
List<Employee> lstEmployee = new List<Employee>()
{
new Employee
{
sID = 100,
Name = "ASHOK"
},
new Employee
{
sID = 101,
Name = "SHAKTHI"
},
new Employee
{
sID = 102,
Name = "RAVI"
},
new Employee
{
sID=100,
Name="ashok"
}
};
var lstEmployeeDetails = lstEmployee.Distinct(new EmployeeComparer());
}
public class Employee
{
public int sID { get; set; }
public string Name { get; set; }
public override bool Equals(object obj)
{
return this.sID == ((Employee)obj).sID && this.Name ==
((Employee)obj).Name;
}
public override int GetHashCode()
{
return this.sID.GetHashCode() ^ this.Name.GetHashCode();
}
}
public class EmployeeComparer : IEqualityComparer<Employee>
{
public bool Equals(Employee x, Employee y)
{
return x.sID == y.sID && x.Name == y.Name;
}
public int GetHashCode(Employee obj)
{
return obj.sID.GetHashCode()^ obj.Name.GetHashCode();
}
}
在列表中重复数据sid = 101两次(重复),小写。我只需要获取“ ASHOK”和id = 101