C# 在列表之间字符串匹配的项目上合并2个列表

C# 在列表之间字符串匹配的项目上合并2个列表,c#,visual-studio,C#,Visual Studio,我有两个Lists,其中包含一个网络名称列表 List<string> nets1 = new List<string>() { "net1", "net2", "net3" }; List<string> nets2 = new List<string>() { "net2", "net3", "net4" }; List nets1=newlist(){“net1”、“net2”、“net3”}; List nets2=新列表(){“net2

我有两个
List
s,其中包含一个网络名称列表

List<string> nets1 = new List<string>() { "net1", "net2", "net3" };
List<string> nets2 = new List<string>() { "net2", "net3", "net4" };
List nets1=newlist(){“net1”、“net2”、“net3”};
List nets2=新列表(){“net2”、“net3”、“net4”};
我想将它们组合成一个新的
列表
,但仅限于
字符串
相等的地方。因此,我期望的结果将是
List
类型,并且只包含
net2
net3


我曾尝试使用Union和Concat,但它们似乎不是我想要的

您想要的是:


您需要的是:


希望您正在寻找这两个列表中的共同元素,您可以使用


希望您正在寻找这两个列表中的共同元素,您可以使用


13秒快:P13秒快:P
var list = list1.Intersect(list2).ToList();
var commonElements = nets1.Intersect(nets2).ToList();