How to get a distinct list from a List of objects?

后端 未结 10 1265
滥情空心
滥情空心 2020-12-24 04:59

I have a List someList.

class MyClass
{
    public int Prop1...
    public int Prop2...
    public int Prop3...
}
10条回答
  •  渐次进展
    2020-12-24 05:17

    you need to use .Distinct(..); extension method. Here's a quick sample:

    public class Comparer : IEqualityComparer
        {
            public bool Equals(Point x, Point y)
            {
                return x.X == y.X;
            }
    
            public int GetHashCode(Point obj)
            {
                return (int)obj.X;
            }
        }
    

    Do not forget about GetHashCode.

    Usage:

    List p = new List();
    // add items
    p.Distinct(new Comparer());
    

提交回复
热议问题