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

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

add a list into another list in vb.net

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...