[英]Select similar items of different lists using using C# and Linq
In an asp.net mvc application, I am trying to perform a selection based on what user selects from product specifications.在 asp.net mvc 应用程序中,我试图根据用户从产品规格中选择的内容进行选择。 Imagine a user selected these parameters as a selection:
想象一下,用户选择了这些参数作为选择:
manufacturer: [apple, sony, samsung]
ram: [8, 128, 256]
size: [12, 13.5]
based on that, lets say the selection resulted in lists of product Ids.基于此,假设选择产生了产品 ID 列表。
list1={10,12,18,100}
list2={10,18,20,21,100,102}
list3={1,2,9,10,12,18,100}
the result should be common Ids:结果应该是常见的 ID:
result={10,18,100}
Since there might be more than 3 lists, is there a Linq command for any number of lists?由于可能有 3 个以上的列表,是否有适用于任意数量列表的 Linq 命令?
You can use the Intersect
method provided by .NET.您可以使用 .NET 提供的
Intersect
方法。 Here's the documentation about the usage这是有关用法的文档
Use intersect to get similar item from Two Lists.使用相交从两个列表中获取相似的项目。
List<string> Fruits = new List<string>();
Fruits.Add("Apple");
Fruits.Add("Mango");
Fruits.Add("Grapes");
Fruits.Add("Banana");
Fruits.Add("Orange");
Fruits.Add("Sweet Potato");
List<string> Vegetables = new List<string>();
Vegetables.Add("Tomato");
Vegetables.Add("Potato");
Vegetables.Add("Onion");
Vegetables.Add("Apple");
Vegetables.Add("Orange");
Vegetables.Add("Banana");
var Data = Fruits.Intersect(Vegetables).ToList();
foreach(var d in Data)
{
Console.WriteLine(d);
}
Output:- Apple Banana Orange输出:-苹果香蕉橙
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.