将一个列表添加到 vb.net 中的另一个列表中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9478600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
add a list into another list in vb.net
提问by Naad Dyr
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?
要获得类似的东西{{r1,a1},{r2,a2},{r3,a3}}
,我怎样才能在 VB.Net 中实现这一点?
采纳答案by Binary Worrier
I assume from your question you want nested Lists, not to simply append one list onto the end of another?
我从你的问题中假设你想要嵌套列表,而不是简单地将一个列表附加到另一个列表的末尾?
Dim listRecord As New List(Of String)
listRecord.Add(txtRating.Text)
listRecord.Add(txtAge.Text)
listRace.Add(listRecord)
Dim records as new List(of List(of String))
records.Add(listRecord)
Hope this helps
希望这可以帮助
Update
Reading them is like accessing any other list.
To get the first field in the first record
更新
阅读它们就像访问任何其他列表。
获取第一条记录中的第一个字段
return records(0)(0)
second field in first record
第一条记录中的第二个字段
return records(0)(1)
etc . . .
等等 。. .
回答by Tim Schmelter
You could use List's AddRange
你可以使用 List 的AddRange
listRace.AddRange(listRecord)
or Enumerable's Concat:
或 Enumerable 的Concat:
Dim allItems = listRace.Concat(listRecord)
Dim newList As List(Of String) = allItems.ToList()
if you want to eliminate duplicates use Enumerable's Union:
如果要消除重复项,请使用 Enumerable's Union:
Dim uniqueItems = listRace.Union(listRecord)
The difference between AddRange
and Concat
is:
之间的区别AddRange
和Concat
是:
Enumerable.Concat
produces a new sequence(well, actually is doesn't produce it immediately due toConcat
's deferred execution, it's more like a query) and you have to useToList
to create a new list from it.List.AddRange
adds them to the sameList
so modifes the original one.
Enumerable.Concat
生成一个新序列(好吧,实际上由于Concat
延迟执行而不会立即生成它,它更像是一个查询)并且您必须使用它ToList
从中创建一个新列表。List.AddRange
将它们添加到同一个,List
因此修改了原始的。
回答by MuhsinFatih
I have been looking for the same problem and I found the solution. I think this is exactly what you want (To set list items inline, instead of using functions of List(of()) class):
我一直在寻找同样的问题,我找到了解决方案。我认为这正是您想要的(设置内联列表项,而不是使用 List(of()) 类的函数):
Dim somelist As New List(Of List(Of String)) From {New List(Of String) From {("L1 item1"), ("L1 item2")}, New List(Of String) From {("L2 item1"), ("L2 item2")}}
I admit that it looks complicated, but this is the structure.
我承认它看起来很复杂,但这就是结构。
In order to make the code look simpler, I add the following screen snip showing the code: https://www.dropbox.com/s/lwym7xq7e2wvwto/Capture12.PNG?dl=0
为了使代码看起来更简单,我添加了以下显示代码的屏幕截图:https://www.dropbox.com/s/lwym7xq7e2wvwto/Capture12.PNG?dl =0