对LINQ和对象使用不同的对象 [英] Using Distinct with LINQ and Objects

查看:61
本文介绍了对LINQ和对象使用不同的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到最近,我还在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆