Using LINQ to find duplicates across multiple properties

前端 未结 3 1168
滥情空心
滥情空心 2020-12-31 00:22

Given a class with the following definition:

public class MyTestClass
{
    public int ValueA { get; set; }
    public int ValueB { get; set; }
}
         


        
3条回答
  •  萌比男神i
    2020-12-31 01:07

    MyTestClass should implement the Equals method.

    public bool Equals(MyTestClass x, MyTestClass y)
    {
        if (Object.ReferenceEquals(x, y)) return true;
    
        if (Object.ReferenceEquals(x, null) ||
            Object.ReferenceEquals(y, null))
                return false;
    
            return x.ValueA == y.ValueA && y.ValueB == y.ValueB;
    }
    

    Here you have a good article about it.

    After that you can get a "clean" list of MyTestClass with "Distinct" method.

提交回复
热议问题