使用GroupBy()
和Count() > 1
我试图在列表中找到我班级的重复实例。
该课程如下:
public class SampleObject
{
public string Id;
public IEnumerable<string> Events;
}
这就是我实例化和分组列表的方式:
public class Program
{
private static void Main(string[] args)
{
var items = new List<SampleObject>()
{
new SampleObject() { Id = "Id", Events = new List<string>() { "ExampleEvent" } },
new SampleObject() { Id = "Id", Events = new List<string>() { "ExampleEvent" } }
};
var duplicates = items.GroupBy(x => new { Token = x.Id, x.Events })
.Where(g => g.Count() > 1)
.Select(g => g.Key)
.ToList();
}
}
duplicates
不包含任何内容。如何进行分组工作?
答案 0 :(得分:13)
要让对象与许多LINQ运算符一起使用,例如GroupBy
或Distinct
,您必须实现GetHashCode
&amp; Equals
,或者您必须提供自定义比较器。
在您的情况下,将属性作为列表,您可能需要一个比较器,除非您将列表设为只读。
试试这个比较器:
public class SampleObjectComparer : IEqualityComparer<SampleObject>
{
public bool Equals(SampleObject x, SampleObject y)
{
return x.Id == y.Id && x.Events.SequenceEqual(y.Events);
}
public int GetHashCode(SampleObject x)
{
return x.Id.GetHashCode() ^ x.Events.Aggregate(0, (a, y) => a ^ y.GetHashCode());
}
}
现在这段代码有效:
var items = new List<SampleObject>()
{
new SampleObject() { Id = "Id", Events = new List<string>() { "ExampleEvent"} },
new SampleObject() { Id = "Id", Events = new List<string>() { "ExampleEvent" } }
};
var comparer = new SampleObjectComparer();
var duplicates = items.GroupBy(x => x, comparer)
.Where(g => g.Count() > 1)
.Select(g => g.Key)
.ToList();
答案 1 :(得分:1)
List<T>
没有被覆盖的Equals
+ GetHashCode
,这就是为什么您的GroupBy
无法正常工作的原因。匿名类型的两个属性之一引用列表,当GroupBy
必须比较时,使用两个列表Object.RefernceEquals
仅检查两个列表是否相同,如果两个列表都包含样本元素则不
您可以提供自定义IEqualityComparer<T>
:
public class IdEventComparer : IEqualityComparer<SampleObject>
{
public bool Equals(SampleObject x, SampleObject y)
{
if (object.ReferenceEquals(x, y))
return true;
if (x == null || y == null)
return false;
if(x.Id != y.Id)
return false;
if (x.Events == null && y.Events == null)
return true;
if (x.Events == null || y.Events == null)
return false;
return x.Events.SequenceEqual(y.Events);
}
public int GetHashCode(SampleObject obj)
{
if(obj == null) return 23;
unchecked
{
int hash = 23;
hash = (hash * 31) + obj.Id == null ? 31 : obj.Id.GetHashCode();
if (obj.Events == null) return hash;
foreach (string item in obj.Events)
{
hash = (hash * 31) + (item == null ? 0 : item.GetHashCode());
}
return hash;
}
}
}
然后你可以在许多LINQ方法中使用它,比如GroupBy
:
var duplicates = items.GroupBy(x => x, new IdEventComparer())
.Where(g => g.Count() > 1)
.Select(g => g.Key)
.ToList();
答案 2 :(得分:1)
GroupBy()
将执行默认比较,导致它找到不相等的列表。
请参阅以下代码:
var eventList1 = new List<string>() { "ExampleEvent" };
var eventList2 = new List<string>() { "ExampleEvent" };
Console.WriteLine(eventList1.GetHashCode());
Console.WriteLine(eventList2.GetHashCode());
Console.WriteLine(eventList1.Equals(eventList2));
两个“平等”名单,对吗?但是,这将打印出来:
796641852
1064243573
False
所以他们不被认为是平等的,因此没有分组。
您需要提供自定义比较器,以比较对象的相关属性。请注意,如前所示,List<T>.GetHashCode()
无法正确表示列表中的项。
您可以这样做(来自Good GetHashCode() override for List of Foo objects respecting the order和LINQ GroupBy on multiple ref-type fields; Custom EqualityComparer):
public class SampleObjectComparer : IEqualityComparer<SampleObject>
{
public bool Equals(SampleObject a, SampleObject b)
{
return a.Id == b.Id
&& a.Events.SequenceEqual(b.Events);
}
public int GetHashCode(SampleObject a)
{
int hash = 17;
hash = hash * 23 + a.Id.GetHashCode();
foreach (var evt in a.Events)
{
hash = hash * 31 + evt.GetHashCode();
}
return hash;
}
}
并像这样使用它:
var eventList1 = new List<string>() { "ExampleEvent" };
var eventList2 = new List<string>() { "ExampleEvent" };
var items = new List<SampleObject>()
{
new SampleObject() { Id = "Id", Events = eventList1 },
new SampleObject() { Id = "Id", Events = eventList2 }
};
var duplicates = items.GroupBy(x => x, new SampleObjectComparer())
.Where(g => g.Count() > 1)
.Select(g => g.Key)
.ToList();
Console.WriteLine(duplicates.Count);