对LINQ和对象使用不同的对象 [英] Using Distinct with LINQ and Objects
问题描述
直到最近,我还在LINQ中使用Distinct从表中选择一个不同的类别(一个枚举).一切正常.
Until recently, I was using a Distinct in LINQ to select a distinct category (an enum) from a table. This was working fine.
我现在需要在包含类别和国家(两个枚举)的类上使它与众不同. Distinct现在无法正常工作.
I now need to have it distinct on a class containing a category and country (both enums). The Distinct isn't working now.
我在做什么错了?
推荐答案
我相信这篇文章可以解释您的问题: http://blog.jordanterrell.com /post/LINQ-Distinct()-does-not-work-as-expected.aspx
I believe this post explains your problem: http://blog.jordanterrell.com/post/LINQ-Distinct()-does-not-work-as-expected.aspx
以上链接的内容可以总结为说可以通过执行以下操作来替换Distinct()方法.
The content of the above link can be summed up by saying that the Distinct() method can be replaced by doing the following.
var distinctItems = items
.GroupBy(x => x.PropertyToCompare)
.Select(x => x.First());
这篇关于对LINQ和对象使用不同的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!