Let's say I've got:
假设我有:
Dim los1 as New List(Of String)
los1.Add("Some value")
Dim los2 as New List(Of String)
los2.Add("More values")
What would be the most efficient way to combine the two into a single List(Of String)
?
将两者合并为一个List(Of String)的最有效方法是什么?
Edit: While I love the solutions everyone has provided, I probably should have also mentioned I'm stuck using the .NET 2.0 framework.
编辑:虽然我喜欢每个人提供的解决方案,但我可能也应该提到我使用.NET 2.0框架。
4 个解决方案
#1
29
I think
我认为
los1.AddRange(los2)
#2
16
If you just want to see a sequence of the two lists then use the Enumerable.Concat
method.
如果您只想查看两个列表的序列,请使用Enumerable.Concat方法。
Dim combined = los1.Concat(los2)
This will return an IEnemurable(Of String)
which contains all of the elements in los1
and los2
. It won't allocate a huge new collection but instead will iterate over the 2 separate collections.
这将返回一个IEnemurable(Of String),其中包含los1和los2中的所有元素。它不会分配一个巨大的新集合,而是迭代2个独立的集合。
#3
6
JaredPar's answer will give you an object that will enumerate over both lists. If you actually want a List(Of String)
object containing these values, it's as simple as:
JaredPar的答案将为您提供一个将枚举两个列表的对象。如果你真的想要一个包含这些值的List(Of String)对象,它就像这样简单:
Dim combined As New List(Of String)(los1.Concat(los2));
EDIT: You know, just because you're using .NET 2.0 doesn't mean you can't roll your own versions of some of the LINQ extension methods you personally find useful. The Concat
method in particular would be quite trivial to implement in C#*:
编辑:你知道,仅仅因为你使用的是.NET 2.0并不意味着你不能推出你自己认为有用的一些LINQ扩展方法的自己版本。特别是Concat方法在C#*中实现起来非常简单:
public static class EnumerableUtils {
public static IEnumerable<T> Concat<T>(IEnumerable<T> first, IEnumerable<T> second) {
foreach (T item in first)
yield return item;
foreach (T item in second)
yield return item;
}
}
Then, see here:
然后,看到这里:
Dim los1 as New List(Of String)
los1.Add("Some value")
Dim los2 as New List(Of String)
los2.Add("More values")
Dim combined As New List(Of String)(EnumerableUtils.Concat(los2, los2))
* To be fair, this is a lot more straightforward in C# thanks to the yield
keyword. It could be done in VB.NET, but it'd be trickier to provide deferred execution in the same manner that the LINQ extensions do.
*公平地说,由于yield关键字,这在C#中要简单得多。它可以在VB.NET中完成,但以LINQ扩展的相同方式提供延迟执行会比较棘手。
#4
1
Union() potentially, if you want a distinct list of entries (I beleive it only does a distinct), is another alternative.
Union()可能,如果你想要一个不同的条目列表(我相信它只是一个独特的),是另一种选择。
#1
29
I think
我认为
los1.AddRange(los2)
#2
16
If you just want to see a sequence of the two lists then use the Enumerable.Concat
method.
如果您只想查看两个列表的序列,请使用Enumerable.Concat方法。
Dim combined = los1.Concat(los2)
This will return an IEnemurable(Of String)
which contains all of the elements in los1
and los2
. It won't allocate a huge new collection but instead will iterate over the 2 separate collections.
这将返回一个IEnemurable(Of String),其中包含los1和los2中的所有元素。它不会分配一个巨大的新集合,而是迭代2个独立的集合。
#3
6
JaredPar's answer will give you an object that will enumerate over both lists. If you actually want a List(Of String)
object containing these values, it's as simple as:
JaredPar的答案将为您提供一个将枚举两个列表的对象。如果你真的想要一个包含这些值的List(Of String)对象,它就像这样简单:
Dim combined As New List(Of String)(los1.Concat(los2));
EDIT: You know, just because you're using .NET 2.0 doesn't mean you can't roll your own versions of some of the LINQ extension methods you personally find useful. The Concat
method in particular would be quite trivial to implement in C#*:
编辑:你知道,仅仅因为你使用的是.NET 2.0并不意味着你不能推出你自己认为有用的一些LINQ扩展方法的自己版本。特别是Concat方法在C#*中实现起来非常简单:
public static class EnumerableUtils {
public static IEnumerable<T> Concat<T>(IEnumerable<T> first, IEnumerable<T> second) {
foreach (T item in first)
yield return item;
foreach (T item in second)
yield return item;
}
}
Then, see here:
然后,看到这里:
Dim los1 as New List(Of String)
los1.Add("Some value")
Dim los2 as New List(Of String)
los2.Add("More values")
Dim combined As New List(Of String)(EnumerableUtils.Concat(los2, los2))
* To be fair, this is a lot more straightforward in C# thanks to the yield
keyword. It could be done in VB.NET, but it'd be trickier to provide deferred execution in the same manner that the LINQ extensions do.
*公平地说,由于yield关键字,这在C#中要简单得多。它可以在VB.NET中完成,但以LINQ扩展的相同方式提供延迟执行会比较棘手。
#4
1
Union() potentially, if you want a distinct list of entries (I beleive it only does a distinct), is another alternative.
Union()可能,如果你想要一个不同的条目列表(我相信它只是一个独特的),是另一种选择。