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

后端 未结 10 1253
滥情空心
滥情空心 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:18

    Unfortunately there's no really easy built-in support for this in the framework - but you can use the DistinctBy implementation I have in MoreLINQ.

    You'd use:

    var distinctList = someList.DistinctBy(x => x.Prop2).ToList();
    

    (You can take just the DistinctBy implementation. If you'd rather use a Microsoft implementation, I believe there's something similar in the System.Interactive assembly of Reactive Extensions.)

提交回复
热议问题