在 LINQ 中与匿名类型不同(在 VB.NET 中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6556077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Distinct in LINQ with anonymous types (in VB.NET)
提问by OrElse
Supposing the referenced Listbelow contains 2 elements:
假设List下面引用的包含2个元素:
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 Countriesquery should contain only
我怎样才能得到不同的值?该Countries查询应该只包含
.Country=Spain .CountryID = 1
回答by Khepri
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).
我只能假设您对使用匿名类型一无所知,因为 Alex Peck 给出的答案是正确的。(我已经赞成)。
However, this boils down to a VB.NET vs C# compiler discussion.
但是,这归结为 VB.NET 与 C# 编译器的讨论。
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.
在 VB.NET 中,当遇到匿名类型时,只有那些声明为关键属性的属性才能用于比较目的。因此,在没有键的 VB.NET 中,当您尝试进行不同的比较时,什么也不会发生。
So first, to answer your question, this works withanonymous types:
首先,要回答您的问题,这适用于匿名类型:
Dim Countries = From c In List Select New With {Key c.CountryId, c.Country} Distinct.ToList


This is why freedompeace's answer doesn't quite work.
这就是为什么自由和平的答案并不完全奏效。
C# however the compiler is a little different.
C# 但是编译器有点不同。
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.
当遇到匿名类型并且需要比较操作时,c# 编译器会覆盖 Equals 和 GetHashCode。它将遍历匿名类型的所有公共属性以计算对象的哈希码以测试是否相等。
And you can read more about that here.
Hope this answers your question.
希望这能回答你的问题。
回答by Alex Peck
Dim distinctCountries = Countries.Distinct()
This works for me when I stop on the last line in the debugger:
当我停在调试器的最后一行时,这对我有用:
Imports System.Text
<TestClass()>
Public Class UnitTest1
Class Test
Public Country As String
Public CountryID As Integer
End Class
<TestMethod()>
Public Sub TestMethod1()
Dim List(1) As Test
List(0) = New Test With {.Country = "Spain", .CountryID = 1}
List(1) = New Test With {.Country = "Spain", .CountryID = 1}
Dim Countries = From c In List Select c.Country, c.CountryID
Dim distinctCountries = Countries.Distinct()
End Sub
End Class
回答by Diana Ionita
回答by Peter Porfy
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:
Distinct 必须以某种方式知道哪些对象是相同的。你在这里选择匿名对象,它不知道哪些是相等的。我从来没有写过一行 VB.Net,但我尝试了一些东西,它有效:
Module Module1
Sub Main()
Dim countries As List(Of Country) = New List(Of Country)
Dim spain1 As Country = New Country()
Dim spain2 As Country = New Country()
Dim spain3 As Country = New Country()
Dim hungary As Country = New Country()
spain1.ID = 1
spain1.Name = "Spain"
spain2.ID = 1
spain2.Name = "Spain"
spain3.ID = 2
spain3.Name = "Spain"
hungary.ID = 3
hungary.Name = "Hungary"
countries.Add(spain1)
countries.Add(spain2)
countries.Add(spain3)
countries.Add(hungary)
Dim ctr = From c In countries Select c.Name, c.ID
Distinct
For Each c In ctr
Console.WriteLine(c)
Next
End Sub
Public Class Country
Protected _name As String
Protected _id As Long
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property ID() As Long
Get
Return _id
End Get
Set(ByVal value As Long)
_id = value
End Set
End Property
End Class
End Module
In your case:
在你的情况下:
Dim Countries = From c In List
Select c.Country, c.CountryID Distinct
回答by foxy
Dim Countries = From c In List _
Select New With {.Country = c.Country, .CountryID = c.CountryID }.Distinct()

