LINQ与匿名类型的区别(在VB.NET中)

时间:2011-07-02 09:06:23

标签: .net vb.net linq linq-to-objects distinct

假设下面引用的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

5 个答案:

答案 0 :(得分:35)

我只能假设你因为使用匿名类型而死定,因为Alex Peck给出的答案是正确的。 (我赞成它)。

然而,这归结为VB.NET与C#编译器的讨论。

在VB.NET中,遇到匿名类型时,只有那些声明为键属性的属性才能用于比较。因此,在没有密钥的VB.NET中,当您尝试进行明确的比较时,不会发生任何事情。

Read all about it here.

首先,要回答您的问题,使用匿名类型:

Dim Countries = From c In List Select New With {Key c.CountryId, c.Country} Distinct.ToList

enter image description here

这就是为什么freedompeace的答案不太有效。

C#然而编译器有点不同。

当遇到匿名类型并且需要进行比较操作时,c#编译器会覆盖Equals和GetHashCode。它将迭代匿名类型的所有公共属性,以计算对象的哈希代码以测试相等性。

And you can read more about that here.

希望这能回答你的问题。

答案 1 :(得分:5)

Dim distinctCountries = Countries.Distinct()

当我停在调试器的最后一行时,这对我有用:

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

答案 2 :(得分:2)

有一个名为Distinct()的LINQ运算符,您可以这样调用:

 Dim Countries = (From c In List _
             Select c.Country, c.CountryID).Distinct()

有关区别here

的更多信息

答案 3 :(得分:2)

区别必须知道哪些对象是相同的。你在这里选择匿名对象,它不知道哪些是相同的。我从来没有写过一行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

在你的情况下:

Dim Countries = From c In List 
                Select c.Country, c.CountryID Distinct

答案 4 :(得分:1)

Dim Countries = From c In List _
                 Select New With {.Country = c.Country, .CountryID = c.CountryID }.Distinct()