是否可以在。net中使用c#将两个或多个列表转换为一个列表?
例如,
public static List<Product> GetAllProducts(int categoryId){ .... }
.
.
.
var productCollection1 = GetAllProducts(CategoryId1);
var productCollection2 = GetAllProducts(CategoryId2);
var productCollection3 = GetAllProducts(CategoryId3);
假设您想要一个包含指定类别id的所有产品的列表,您可以将查询视为一个投影,然后是一个扁平化操作。有一个LINQ操作符做这个:SelectMany。
// implicitly List<Product>
var products = new[] { CategoryId1, CategoryId2, CategoryId3 }
.SelectMany(id => GetAllProducts(id))
.ToList();
在c# 4中,你可以将SelectMany缩短为:.SelectMany(GetAllProducts)
如果您已经拥有代表每个Id的产品的列表,那么正如其他人指出的那样,您需要的是一个连接。
你可以使用LINQ Concat和ToList方法:
var allProducts = productCollection1.Concat(productCollection2)
.Concat(productCollection3)
.ToList();
注意,还有更有效的方法可以做到这一点——上面的方法基本上会遍历所有条目,创建一个动态大小的缓冲区。由于您可以预测开始时的大小,因此不需要这种动态大小…所以你可以用:
var allProducts = new List<Product>(productCollection1.Count +
productCollection2.Count +
productCollection3.Count);
allProducts.AddRange(productCollection1);
allProducts.AddRange(productCollection2);
allProducts.AddRange(productCollection3);
(AddRange用于ICollection<T>以提高效率。)
我不会采用这种方法,除非你真的必须这么做。
列表。AddRange将通过添加额外的元素来改变(突变)一个现有的列表:
list1.AddRange(list2); // list1 now also has list2's items appended to it.
或者,在现代的不可变风格中,你可以在不改变现有列表的情况下投影出一个新的列表:
Concat,它表示list1的项的无序序列,后面跟着list2的项:
var concatenated = list1.Concat(list2).ToList();
不太一样的是,Union投射了一个截然不同的项目序列:
var distinct = list1.Union(list2).ToList();
注意,为了让Union的“值类型不同”行为在引用类型上工作,你需要为你的类定义相等比较(或者使用记录类型的内置比较器)。
我已经对它进行了评论,但我仍然认为这是一个有效的选择,只需测试一下在您的环境中,哪种解决方案更好。在我的例子中,使用source。ForEach(p => dest.Add(p))比经典的AddRange执行得更好,但我没有调查为什么在低级别。
您可以在这里看到示例代码:https://gist.github.com/mcliment/4690433
所以选项是:
var allProducts = new List<Product>(productCollection1.Count +
productCollection2.Count +
productCollection3.Count);
productCollection1.ForEach(p => allProducts.Add(p));
productCollection2.ForEach(p => allProducts.Add(p));
productCollection3.ForEach(p => allProducts.Add(p));
测试一下,看看它是否适合你。
免责声明:我不提倡这个解决方案,我发现Concat是最清楚的一个。我刚刚在与Jon的讨论中说过,在我的机器中,这种情况比AddRange表现得更好,但他说,他的知识比我多得多,这是没有意义的。如果你想比较的话,这就是要点。
将多个列表合并或合并为一个列表。
有一件事必须是正确的:两个列表的类型都是
平等的。
例如:如果我们有一个字符串列表,那么我们可以添加另一个列表到
有string类型列表的现有列表,否则我们不能。
例子:
class Program
{
static void Main(string[] args)
{
List<string> CustomerList_One = new List<string>
{
"James",
"Scott",
"Mark",
"John",
"Sara",
"Mary",
"William",
"Broad",
"Ben",
"Rich",
"Hack",
"Bob"
};
List<string> CustomerList_Two = new List<string>
{
"Perter",
"Parker",
"Bond",
"been",
"Bilbo",
"Cooper"
};
// Adding all contents of CustomerList_Two to CustomerList_One.
CustomerList_One.AddRange(CustomerList_Two);
// Creating another Listlist and assigning all Contents of CustomerList_One.
List<string> AllCustomers = new List<string>();
foreach (var item in CustomerList_One)
{
AllCustomers.Add(item);
}
// Removing CustomerList_One & CustomerList_Two.
CustomerList_One = null;
CustomerList_Two = null;
// CustomerList_One & CustomerList_Two -- (Garbage Collected)
GC.Collect();
Console.WriteLine("Total No. of Customers : " + AllCustomers.Count());
Console.WriteLine("-------------------------------------------------");
foreach (var customer in AllCustomers)
{
Console.WriteLine("Customer : " + customer);
}
Console.WriteLine("-------------------------------------------------");
}
}