[英]How to get a distinct list from a List of objects?
我有一个List<MyClass> someList
。
class MyClass
{
public int Prop1...
public int Prop2...
public int Prop3...
}
我想知道如何从List<MyClass> someList
获得一个新的不同List<MyClass> distinctList
,但只将它与Prop2
进行比较。
您可以使用GroupBy
模拟DistinctBy
的效果,然后仅使用每个组中的第一个条目。 虽然可能比其他实现慢一点。
someList.GroupBy(elem=>elem.Prop2).Select(group=>group.First());
不幸的是,在框架中并没有真正简单的内置支持——但您可以使用我在MoreLINQ中的DistinctBy
实现。
你会使用:
var distinctList = someList.DistinctBy(x => x.Prop2).ToList();
(您可以只采用DistinctBy
实现。如果您更愿意使用 Microsoft 实现,我相信Reactive Extensions的 System.Interactive 程序集中有类似的东西。)
你需要使用.Distinct(..);
扩展方法。 这是一个快速示例:
public class Comparer : IEqualityComparer<Point>
{
public bool Equals(Point x, Point y)
{
return x.X == y.X;
}
public int GetHashCode(Point obj)
{
return (int)obj.X;
}
}
不要忘记GetHashCode
。
用法:
List<Point> p = new List<Point>();
// add items
p.Distinct(new Comparer());
覆盖Equals(object obj)和GetHashCode()方法:
class MyClass
{
public int Prop1 { get; set; }
public int Prop2 { get; set; }
public int Prop3 { get; set; }
public override bool Equals(object obj)
{
return ((MyClass)obj).Prop2 == Prop2;
}
public override int GetHashCode()
{
return Prop2.GetHashCode();
}
}
然后只需调用:
List<MyClass> distinctList = someList.Distinct().ToList();
自从引入了值元组,如果你想要一个相当于 SQL 的 DISTINCT 的 LINQ
items.GroupBy(item => (item.prop1, item.prop2, ...)).Select(group => group.First())
如果你想通过多个字段Distinct
你的列表,你必须创建一个IEqualityComparer
接口的实例:
public class MyComparer : IEqualityComparer<MyModel>
{
public bool Equals(MyModel x, MyModel y)
{
// compare multiple fields
return
x.Field1 == y.Field1 &&
x.Field2 == y.Field2 &&
x.Field3 == y.Field3 ;
}
public int GetHashCode(MyModel obj)
{
return
obj.Field1.GetHashCode() +
obj.Field2.GetHashCode() +
obj.Field3.GetHashCode();
}
}
然后使用比较器来区分您的列表:
var distinctedList = myList.Distinct(new MyComparer()).ToList();
创建一个实现IEqualityComparer接口的类,该接口仅检查您的 Prop2-Property。 然后,您可以将此类的实例传递给 Distinct 扩展方法。
我知道这已经有一段时间了,但我需要最简单的答案,此时(使用 .NET 4.5.1)我发现以下是我能得到的最直接的答案:
IEnumerable<long> allIds = waitingFiles.Values.Select(wf => wf.groupId).Distinct();
我的情况是我有一个看起来像这样的 ConcurrentDictionary: ConcurrentDictionary<long, FileModel>
ConcurrentDictionary Values 属性基本上是我的List<FileModel>
。
*FileModel 有一个不一定唯一的 groupId(尽管,显然我用来将 FileModel 对象添加到字典中的键(长)对于 FileModel 是唯一的)。
*为示例中的清晰起见命名。
关键是我在 ConcurrentDictionary 中有大量的 FileModel(假设有 100 个),在这 100 个 FileModel 中有 5 个不同的 groupId。
此时我只需要一个不同 groupId 的列表。
所以,如果我只有一个 FileModel 列表,代码将如下所示:
IEnumerable <long> allIds = allFileModel.Select(fm => fm.groupId).Distinct();
我有一个List<MyClass> someList
。
class MyClass
{
public int Prop1...
public int Prop2...
public int Prop3...
}
我想知道如何从List<MyClass> someList
获得一个新的不同List<MyClass> distinctList
,但只将它与Prop2
进行比较。
删除所有属性都相等的重复项的简单方法:
System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
serviceList = serviceList.GroupBy(s => jss.Serialize(s)).Select(group => group.First()).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.