Given a class with the following definition:
给定具有以下定义的类:
public class MyTestClass
{
public int ValueA { get; set; }
public int ValueB { get; set; }
}
How can duplicate values be found in a MyTestClass[] array?
如何在MyTestClass []数组中找到重复值?
For example,
例如,
MyTestClass[] items = new MyTestClass[3];
items[0] = new MyTestClass { ValueA = 1, ValueB = 1 };
items[1] = new MyTestClass { ValueA = 0, ValueB = 1 };
items[2] = new MyTestClass { ValueA = 1, ValueB = 1 };
Contains a duplicate as there are two MyTestClass objects where ValueA and ValueB both = 1
包含副本,因为有两个MyTestClass对象,其中ValueA和ValueB都= 1
3 个解决方案
#1
39
You can find your duplicates by grouping your elements by ValueA and ValueB. Do a count on them afterwards and you will find which ones are duplicates.
您可以通过ValueA和ValueB对元素进行分组来找到重复项。之后再计算它们,你会发现哪些是重复的。
This is how you would isolate the dupes :
这就是你如何孤立欺骗:
var duplicates = items.GroupBy(i => new {i.ValueA, i.ValueB})
.Where(g => g.Count() > 1)
.Select(g => g.Key);
#2
3
You could just use Jon Skeet's DistinctBy
and Except
together to find duplicates. See this Response for his explanation of DistinctBy
.
您可以使用Jon Skeet的DistinctBy和Except一起查找重复项。有关他对DistinctBy的解释,请参阅此响应。
MyTestClass[] items = new MyTestClass[3];
items[0] = new MyTestClass { ValueA = 1, ValueB = 1 };
items[1] = new MyTestClass { ValueA = 0, ValueB = 1 };
items[2] = new MyTestClass { ValueA = 1, ValueB = 1 };
MyTestClass [] distinctItems = items.DistinctBy(p => new {p.ValueA, p.ValueB}).ToArray();
MyTestClass [] duplicates = items.Except(distinctItems).ToArray();
It will only return one item and not both duplicates however.
它只会返回一个项目而不是两个重复项目。
#3
1
MyTestClass should implement the Equals method.
MyTestClass应该实现Equals方法。
public bool Equals(MyTestClass x, MyTestClass y)
{
if (Object.ReferenceEquals(x, y)) return true;
if (Object.ReferenceEquals(x, null) ||
Object.ReferenceEquals(y, null))
return false;
return x.ValueA == y.ValueA && y.ValueB == y.ValueB;
}
Here you have a good article about it.
在这里你有一篇很好的文章。
After that you can get a "clean" list of MyTestClass with "Distinct" method.
之后,您可以使用“Distinct”方法获取MyTestClass的“干净”列表。
#1
39
You can find your duplicates by grouping your elements by ValueA and ValueB. Do a count on them afterwards and you will find which ones are duplicates.
您可以通过ValueA和ValueB对元素进行分组来找到重复项。之后再计算它们,你会发现哪些是重复的。
This is how you would isolate the dupes :
这就是你如何孤立欺骗:
var duplicates = items.GroupBy(i => new {i.ValueA, i.ValueB})
.Where(g => g.Count() > 1)
.Select(g => g.Key);
#2
3
You could just use Jon Skeet's DistinctBy
and Except
together to find duplicates. See this Response for his explanation of DistinctBy
.
您可以使用Jon Skeet的DistinctBy和Except一起查找重复项。有关他对DistinctBy的解释,请参阅此响应。
MyTestClass[] items = new MyTestClass[3];
items[0] = new MyTestClass { ValueA = 1, ValueB = 1 };
items[1] = new MyTestClass { ValueA = 0, ValueB = 1 };
items[2] = new MyTestClass { ValueA = 1, ValueB = 1 };
MyTestClass [] distinctItems = items.DistinctBy(p => new {p.ValueA, p.ValueB}).ToArray();
MyTestClass [] duplicates = items.Except(distinctItems).ToArray();
It will only return one item and not both duplicates however.
它只会返回一个项目而不是两个重复项目。
#3
1
MyTestClass should implement the Equals method.
MyTestClass应该实现Equals方法。
public bool Equals(MyTestClass x, MyTestClass y)
{
if (Object.ReferenceEquals(x, y)) return true;
if (Object.ReferenceEquals(x, null) ||
Object.ReferenceEquals(y, null))
return false;
return x.ValueA == y.ValueA && y.ValueB == y.ValueB;
}
Here you have a good article about it.
在这里你有一篇很好的文章。
After that you can get a "clean" list of MyTestClass with "Distinct" method.
之后,您可以使用“Distinct”方法获取MyTestClass的“干净”列表。