问题描述
我正在读一本有关Linq的书,发现Distinct方法有一个需要比较器的重载.这将是一个解决我遇到的问题的好方法,我想从集合中获取不同的实体,但是即使其他属性不同,也希望对实体ID进行比较.
I was reading a book about Linq, and saw that the Distinct method has an overload that takes a comparer. This would be a good solution to a problem I have where I want to get the distinct entities from a collection, but want the comparison to be on the entity ID, even if the other properties are different.
根据这本书,如果我有一个Gribulator实体,我应该能够创建一个像这样的比较器...
According to the book, if I have a Gribulator entity, I should be able to create a comparer like this...
private class GribulatorComparer : IComparer<Gribulator> {
public int Compare(Gribulator g1, Gribulator g2) {
return g1.ID.CompareTo(g2.ID);
}
}
...然后像这样使用它...
...and then use it like this...
List<Gribulator> distinctGribulators
= myGribulators.Distinct(new GribulatorComparer()).ToList();
但是,这会导致以下编译器错误...
However, this gives the following compiler errors...
'System.Collections.Generic.List'不包含'Distinct'的定义,最佳扩展方法重载'System.Linq.Enumerable.Distinct(System.Collections.Generic.IEnumerable,System.Collections.Generic. IEqualityComparer)'有一些无效的参数
'System.Collections.Generic.List' does not contain a definition for 'Distinct' and the best extension method overload 'System.Linq.Enumerable.Distinct(System.Collections.Generic.IEnumerable, System.Collections.Generic.IEqualityComparer)' has some invalid arguments
参数2:无法从"LinqPlayground.Program.GribulatorComparer"转换为"System.Collections.Generic.IEqualityComparer"
Argument 2: cannot convert from 'LinqPlayground.Program.GribulatorComparer' to 'System.Collections.Generic.IEqualityComparer'
我进行了一些搜索,看到了很多使用此类代码的示例,但没有抱怨编译器错误.
I've searched around a bit, and have seen plenty of examples that use code like this, but no complaints about compiler errors.
我做错了什么?另外,这是最好的方法吗?我在这里想要一个一次性的解决方案,所以不想开始更改实体本身的代码.我希望该实体保持正常状态,但仅在这一位置,仅按ID比较即可.
What am I doing wrong? Also, is this the best way of doing this? I want a one-off solution here, so don't want to start changing the code for the entity itself. I want the entity to remain as normal, but just in this one place, compare by ID only.
感谢您的帮助.
推荐答案
您正在将比较器实现为IComparer<T>
,LINQ方法重载需要实现IEqualityComparer
:
You're implementing your comparer as an IComparer<T>
, the LINQ method overload requires an implementation of IEqualityComparer
:
private class GribulatorComparer : IEqualityComparer<Gribulator> {
public bool Equals(Gribulator g1, Gribulator g2) {
return g1.ID == g2.ID;
}
}
为澄清起见,IComparer
接口可用于排序,因为这基本上是Compare()
方法的作用.
For clarification, the IComparer
interface can be used for sorting, as that's basically what the Compare()
method does.
赞:
items.OrderBy(x => new ItemComparer());
private class ItemComparer : IComparer<Item>
{
public int Compare(Item x, Item y)
{
return x.Id.CompareTo(y.Id)
}
}
哪个将使用该比较器对集合进行排序,但是LINQ提供了一种对简单字段(例如int Id)执行此操作的方法.
Which will sort your collection using that comparer, however LINQ provides a way to do that for simple fields (like an int Id).
items.OrderBy(x => x.Id);
这篇关于如何将自定义比较器与Linq Distinct方法一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!