VB.Net: 将一个列表的内容放入另一个列表

VB.Net: Putting Contents of One List into Another

给定两个列表:

Dim listOfCollectiveTasks As New List(Of String) Dim listOfCollectiveTasks2 As New List(Of String)

我正在向 listOfCollectiveTasks2 添加元素,然后尝试删除 listOfCollectiveTasks 的内容...然后将 listOfCollectiveTasks2 的内容放入其中。

代码:

Public Sub testRecursive()
    For Each atask In listOfCollectiveTasks
        getRemainingSupportingTask(getTaskID(atask), "Collective")
        'getRemainingSupportingTask(atask, "Individual")
    Next


    listOfCollectiveTasks = Nothing
    For Each item In listOfCollectiveTasks2
        Console.WriteLine(item.ToString)
    Next
    listOfCollectiveTasks.Concat(listOfCollectiveTasks2)
    testRecursive()

End Sub

Public Sub getRemainingSupportingTask(theID As String, collectiveOrSupporting As String)
    Dim rs As New ADODB.Recordset
    Dim listOfTasks As New List(Of String)
    Dim theQuery As String = "Select * from [SupportingTask$] where [TaskID] = "
    theQuery = theQuery + "'" + theID + "'" + " and [SupportingTaskTypeName] = " + "'" + collectiveOrSupporting + "'"
    rs = MyADO.executeQuery(theQuery)
    Console.WriteLine(theQuery)


    Do Until rs.EOF
        Dim theString As String = rs.Fields("SupportingTaskNumber").Value
        If String.Equals(collectiveOrSupporting, "Collective") Then

            listOfCollectiveTasks2.Add(theString)



            Console.WriteLine(theString)
            newXlSheet.Cells(lastCollectiveRow, 1) = theString
            lastCollectiveRow = lastCollectiveRow + 1


        Else
            'listOfIndividualTasks.Add(theString)
            Console.WriteLine(theString)
            newXlSheet.Cells(lastIndividualRow, 2) = theString
            lastIndividualRow = lastIndividualRow + 1

        End If
        rs.MoveNext()
    Loop
    Console.WriteLine("Done")
    Console.WriteLine("")



End Sub

我在 `listOfCollectiveTasks.Concat(listOfCollectiveTasks2) 上收到 System.ArgumentNullException。我想不出将 list2 中的项目放入 list1 的方法。

除非您必须在市场之后或市场操作之前做一些事情,否则只添加一个包含 System.Type 集合的范围会更有效。然后清除追加的原始列表。像这样:

Sub Main()
    Dim ls1 = New List(Of String)({"a", "b", "c", "d"})
    Dim ls2 = New List(Of String)
    Dim ls3 = New List(Of String)({"e", "f"})

    ls2.AddRange(ls1)
    ls1.Clear()

    ls2.AddRange(ls3)
    ls3.Clear()

    Console.WriteLine($"Count of List1: {ls1.Count}")
    Console.WriteLine($"Count of List2: {ls2.Count}")
    Console.WriteLine($"Count of List3: {ls3.Count}")
    ls2.ForEach(Sub(x) Console.WriteLine(x))

    Console.ReadLine()
End Sub