如何从对象列表的不同列表? [英] How to get a distinct list from a List of objects?
问题描述
我有一个列表与LT; MyClass的> someList
。
class MyClass
{
public int Prop1...
public int Prop2...
public int Prop3...
}
我想知道如何获得一个新的独特的列表< MyClass的> distinctList
从列表< MyClass的> someList
,而只是把它比作 Prop2
。
I would like to know how to get a new distinct List<MyClass> distinctList
from List<MyClass> someList
, but only comparing it to Prop2
.
推荐答案
不幸的是有没有很容易的内置支持这一框架中的 - 但你可以使用的 DistinctBy
实现我在 MoreLINQ 。
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.
您会使用:
var distinctList = someList.DistinctBy(x => x.Prop2).ToList();
(你可以在 DistinctBy
实施如果你想用微软的实施,我相信有在无扩展的System.Interactive装配类似。)
(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.)
这篇关于如何从对象列表的不同列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!