抽象 IEqualityComparer 实现或覆盖默认比较器以使用 Distinct 方法
我试图在给定 List
的情况下找到一个不同的 List
,其中每个 BlogPost
都有一个 Author属性。我在泛型中找到了
Distinct()
扩展方法,并且正在尝试使用它。首先,让我解释一下我的循环以及我想在哪里使用它,然后我将解释我的类以及我遇到问题的地方。
尝试在此处使用不同
public List<Author> GetAuthors() {
List<BlogPost> posts = GetBlogPosts();
var authors = new List<Author>();
foreach (var bp in posts) {
authors.Add(bp.Author);
}
return authors.Distinct().ToList();
}
根据我阅读的内容在 MSDN 上,Distinct()
要么使用默认比较器,要么使用传入的比较器。我希望(我显然不知道这是否可行)在一个地方编写一个比较器,并能够将它用于我的所有类,因为它们都通过完全相同的相等操作进行比较(比较 每个类的 GUID
属性)。
我的所有类都继承自 BasePage
类:
public class BasePage : System.Web.UI.Page, IBaseTemplate, IEquatable<IBaseTemplate>, IEqualityComparer<IBaseTemplate>
public class Author : BasePage
public class BlogPost : BasePage
在 BasePage
中实现的 equals 方法会比较每个唯一的 GUID
属性。当我在 Author
上调用 Distinct()
时,它似乎不起作用。有什么方法可以将比较器包装在一个地方并且始终能够使用它,而不必编写类似 class AuhorComparer : IEqualityComparer
的内容,因为我需要编写每次我想使用 Distinct()
时,每个类都会做同样的事情。 或者我可以以某种方式覆盖默认比较器,这样我就不必将任何内容传递给Distinct()
吗?
I'm trying to find a distinct List<Author>
given a List<BlogPost>
where each BlogPost
has an Author
property. I've found the Distinct()
extension method in generics and I'm trying to use it. First, let me explain my loop and where I want to use it, then I'll explain my classes and where I'm having trouble.
Trying to use distinct here
public List<Author> GetAuthors() {
List<BlogPost> posts = GetBlogPosts();
var authors = new List<Author>();
foreach (var bp in posts) {
authors.Add(bp.Author);
}
return authors.Distinct().ToList();
}
Based on what I've read on MSDN, Distinct()
either uses the default comparer or a passed in comparer. I was hoping (I obviosuly don't know if this is doable) to write a comparer in one spot and be able to use it for all of my classes since they all compare by the exact same equality operation (which compares the GUID
property of each class).
All of my classes inherit from the BasePage
class:
public class BasePage : System.Web.UI.Page, IBaseTemplate, IEquatable<IBaseTemplate>, IEqualityComparer<IBaseTemplate>
public class Author : BasePage
public class BlogPost : BasePage
My equals method implemented in BasePage
compares the GUID
property which is unique to each. When I call Distinct()
on an Author
it doesn't seem to work. Is there any way I can wrap up the comparer in one place and always be able to use it rather than having to write something like class AuhorComparer : IEqualityComparer<Auhor>
since I'd then need to write the same thing for each class, every time I want to use Distinct()
. Or can I override the default comparer somehow so I don't have to pass anything to Distinct()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Distinct
操作可能不是这里的最佳解决方案,因为您最终会构建一个可能非常大的包含重复项的列表,然后立即将其缩小为不同的元素。最好从HashSet
开始,以避免构建大型列表。如果您确实想使用
Distinct
,那么最好的途径是在Author
类型上实现IEquatable
。如果没有给出显式的IEqualityComparer
,Distinct
和其他 LINQ 方法最终将默认使用该类型的IEquatable
实现。通常通过EqualityComprare.Default
The
Distinct
operation is probably not the best solution here because you end up building a potentially very big list with duplicates only to then immediately shrink it to distinct elements. It's probably better to just start with aHashSet<Author>
to avoid building up the large list.If you do want to use
Distinct
then the best route is to implementIEquatable
on theAuthor
type. When not given an explicitIEqualityComparer
theDistinct
and other LINQ methods will eventually default into using theIEquatable
implementation on the type. Usually throughEqualityComprare<T>.Default
Overriden Equals 应该适合你。可能出现问题的一件事是 GetHashCode 没有与 Equals 一起被覆盖,而框架指南规定应该发生这种情况。
Overriden Equals should work for you. One thing that might be going wrong is that GetHashCode is not overridden alongside Equals, which the framework guidelines dictate should happen.
代码仅显示了主要思想,我希望这会有用。
The code only shows the main idea, which, I hope, will be useful.