add a list into another list in vb.net

前端 未结 3 1558
伪装坚强ぢ
伪装坚强ぢ 2020-12-17 19:54

I have a list as follows and I want to add it in another list:

Dim listRecord As New List(Of String)
listRecord.Add(txtRating.Text)
listRecord.Add(txtAge.Tex         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-17 20:32

    You could use List's AddRange

    listRace.AddRange(listRecord)
    

    or Enumerable's Concat:

    Dim allItems = listRace.Concat(listRecord)
    Dim newList As List(Of String) = allItems.ToList()
    

    if you want to eliminate duplicates use Enumerable's Union:

    Dim uniqueItems = listRace.Union(listRecord)
    

    The difference between AddRange and Concat is:

    • Enumerable.Concat produces a new sequence(well, actually is doesn't produce it immediately due to Concat's deferred execution, it's more like a query) and you have to use ToList to create a new list from it.
    • List.AddRange adds them to the same List so modifes the original one.

提交回复
热议问题