使用与LINQ和对象不同的

Tim*_*ond 16 linq distinct

直到最近,我在LINQ中使用Distinct从表中选择一个不同的类别(枚举).这工作正常.

我现在需要在包含类别和国家(两个枚举)的类中区分它.The Distinct现在不起作用.

我究竟做错了什么?

Sti*_*gar 21

我相信这篇文章解释了你的问题:http: //blog.jordanterrell.com/post/LINQ-Distinct()-does-not-work-as-expected.aspx

上述链接的内容可以通过说明可以通过执行以下操作来替换Distinct()方法来总结.

var distinctItems = items
       .GroupBy(x => x.PropertyToCompare)
       .Select(x => x.First());
Run Code Online (Sandbox Code Playgroud)

  • 是的,我不再这样做了,但在 2010 年我没有注意这种做法。 (6认同)