Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.Text)
listRace.Add(listRecord)  

to obtain something like {{r1,a1},{r2,a2},{r3,a3}}, how can I achieve this in VB.Net?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.0k views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...