LINQ 中与匿名类型不同(在 VB.NET 中)
假设下面引用的 List
包含 2 个元素:
Dim Countries = From c In List _
Select New With { .Country = c.Country, .CountryID = c.CountryID }
上面的代码返回
.Country=Spain .CountryID = 1
.Country=Spain .CountryID = 1
如何获取不同的值? Countries
查询应仅包含
.Country=Spain .CountryID = 1
Supposing the referenced List
below contains 2 elements:
Dim Countries = From c In List _
Select New With { .Country = c.Country, .CountryID = c.CountryID }
the code above returns
.Country=Spain .CountryID = 1
.Country=Spain .CountryID = 1
How can i get the distinct values? The Countries
query should contain only
.Country=Spain .CountryID = 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我只能假设您坚决使用匿名类型,因为 Alex Peck 给出的答案是正确的。 (我已经投了赞成票)。
然而,这归结为 VB.NET 与 C# 编译器的讨论。
在 VB.NET 中,当遇到匿名类型时,只有那些声明为关键属性的属性可用于比较目的。因此,在没有键的 VB.NET 中,当您尝试进行不同比较时,不会发生任何事情。
在此处阅读所有相关内容。
首先,回答您的问题,这适用于匿名类型:
这就是 Freedompeace 的答案不太有效的原因。
然而 C# 的编译器有点不同。
当遇到匿名类型并且需要比较操作时,C# 编译器会重写 Equals 和 GetHashCode。它将迭代匿名类型的所有公共属性来计算对象的哈希码以测试相等性。
您可以阅读更多内容关于这一点,请点击此处。
希望这能回答您的问题。
I can only assume you're dead set on the use of anonymous type as the answer given by Alex Peck is correct. (and I've upvoted it).
However, this boils down to a VB.NET vs C# compiler discussion.
In VB.NET, when an anonymous type is encountered only those properties declared as key properties can be used for comparison purposes. So in VB.NET without key, when you're attempting to do a distinct comparison, nothing will occur.
Read all about it here.
So first, to answer your question, this works with anonymous types:
This is why freedompeace's answer doesn't quite work.
C# however the compiler is a little different.
When an anonymous type is encountered and a comparison operation is needed the c# compiler overrides Equals and GetHashCode. It will iterate over all of the public properties of the anonymous type to compute the object's hash code to test for equality.
And you can read more about that here.
Hope this answers your question.
当我停在调试器的最后一行时,这对我有用:
This works for me when I stop on the last line in the debugger:
Distinct 必须以某种方式知道哪些对象是相同的。你在这里选择匿名对象,它不知道哪些是相等的。我从来没有写过一行 VB.Net,但我尝试了一些东西,它起作用了:
在你的情况下:
Distinct must know somehow which objects are the same. You select anonymus objects here, it doesn't know which are equal. I never wrote a single line of VB.Net, but I tried something, and it works:
In your case:
有一个名为 Distinct() 的 LINQ 运算符,您可以像这样调用它:
有关 Distinct 的更多信息
There is the LINQ operator named Distinct(), which you can call like so:
More information on Distinct here