2

在 vb.net 中,使用 linq,加入 2 个对象列表。两个列表都是同一类型。

我搜索了按 ID 连接两个列表分组的最佳方法。像一个独特的。

样本 :

Imports System.Text
Imports System.Reflection

Module Module1

    Sub Main()
        Dim list1 As New List(Of Item)
        For i As Integer = 1 To 4
            list1.Add(New Item(i, i.ToString))
        Next

        Dim list2 As New List(Of Item)
        For i As Integer = 3 To 6
            list2.Add(New Item(i, i.ToString))
        Next

        Dim list3 As New List(Of Item)
        list3 = (list1.Concat(list2)).ToList
        Console.WriteLine(Item.PrintList(list3).ToString)        

        Console.ReadLine()
    End Sub

End Module

Class Item
    Private _Id As Integer
    Private _Value As String

    Public Property Id() As Integer
        Get
            Return _Id
        End Get
        Set(ByVal value As Integer)
            _Id = value
        End Set
    End Property
    Public Property Value() As String
        Get
            Return _Value
        End Get
        Set(ByVal value As String)
            _Value = value
        End Set
    End Property

    Public Sub New()
    End Sub
    Public Sub New(ByVal id As Integer, ByVal value As String)
        Me.Id = id
        Me.Value = value
    End Sub

    Public Overrides Function ToString() As String
        Dim sb = New Text.StringBuilder()        
        For Each item In [GetType]().GetFields(BindingFlags.NonPublic Or BindingFlags.Instance)
            sb.Append(String.Format("[{0} = {1}] ", item.Name, item.GetValue(Me)))
        Next
        Return sb.ToString()
    End Function

    Public Shared Function PrintList(ByVal myList As List(Of Item)) As StringBuilder
        Dim result As New StringBuilder
        For Each i In myList
            result.AppendLine(i.ToString)
        Next
        Return result
    End Function

End Class

list3 在 concat 之后包含的内容:

[_Id = 1] [_Value = 1] 
[_Id = 2] [_Value = 2] 
[_Id = 3] [_Value = 3] 
[_Id = 4] [_Value = 4] 
[_Id = 3] [_Value = 3] 
[_Id = 4] [_Value = 4] 
[_Id = 5] [_Value = 5] 
[_Id = 6] [_Value = 6] 

我们想要什么:

[_Id = 1] [_Value = 1] 
[_Id = 2] [_Value = 2] 
[_Id = 3] [_Value = 3] 
[_Id = 4] [_Value = 4] 
[_Id = 5] [_Value = 5] 
[_Id = 6] [_Value = 6] 
4

2 回答 2

4

我搜索了按 ID 连接两个列表分组的最佳方法。像一个独特的。

使用Enumerable.Union.

但是,您需要先在类中覆盖Equals和。然后所有 Linq 方法都将使用您的 ID:GetHashCodeItem

Class Item
    Public Property ID As Int32

    Public Overrides Function Equals(obj As Object) As Boolean
        If obj Is Nothing Then Return False
        If Not TypeOf obj Is Item Then Return False
        Return Id = DirectCast(obj, Item).Id
    End Function

    Public Overrides Function GetHashCode() As Integer
        Return Id.GetHashCode()
    End Function
End Class

现在您可以使用Union高效且清晰的:

Dim list3 = list1.Union(list2).ToList

MSDN

此方法从返回集中排除重复项。这与该方法的行为不同,该Concat<TSource>方法返回输入序列中的所有元素,包括重复项。...要比较自定义数据类型,您需要实现此接口并为该类型提供自己的GetHashCodeEquals方法。

于 2013-06-03T22:12:47.950 回答
1

随着在 Tim Schmelter 答案中添加 Equals 和 GetHashCode,您现在还可以直接在 Query 上使用 Concat 和 .Distinct :

Dim list3 As List(Of Item) = list1.Concat(list2).Distinct.ToList

或在查询中使用 where 和 distinct:

Dim list3 As List(Of Item) = (From data In list1.Concat(list2) Where data.Value = "MyCity" Distinct).ToList

变化:

  Dim list3 As List(Of Item) = (From data In list1.Concat(list2) Where data.Value = "MyCity").Distinct.ToList
于 2013-06-04T00:03:00.247 回答