Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq检查另一个列表中的所有值是否存在_C#_Linq_List - Fatal编程技术网

C# Linq检查另一个列表中的所有值是否存在

C# Linq检查另一个列表中的所有值是否存在,c#,linq,list,C#,Linq,List,我有如下的值列表 清单1 -- Id Country market 1 AU 2 2 NZ 2 3 GB 3 4 GG 3 我有另一份清单,如下所示 清单2 我想从列表2中找出国家满足列表1和市场的位置。这意味着我需要从列表2中提取“2”作为位置,因为位置2满足值(即AU和NZ),而位置3未通过验证,因为并非所有国家(来自列表1)都在列表2中 我想知道我们是否可以在LINQ声明中写下这一点。我可以使用循环来找出每个匹配条件

我有如下的值列表

清单1

--
Id Country market

1   AU       2

2   NZ       2

3   GB       3

4   GG       3
我有另一份清单,如下所示

清单2 我想从列表2中找出国家满足列表1和市场的位置。这意味着我需要从列表2中提取“2”作为位置,因为位置2满足值(即AU和NZ),而位置3未通过验证,因为并非所有国家(来自列表1)都在列表2中


我想知道我们是否可以在LINQ声明中写下这一点。我可以使用循环来找出每个匹配条件,但我正在考虑更简单、更好的LINQ编写方法。

如果您只想知道
List1
中存在的所有
List2
条目,那么您可以这样做

bool allexist = list1.All(l=> list2.Any(x=> x.location == l.market && x.Country == l.Country));
或者,如果您想知道所有
List1
条目都存在于
List2
中,那么

var existsinBoth = list1.Where(l=> list2.Any(x=> x.location == l.market && x.Country == l.Country));

如果您只想知道
List1
中存在的所有
List2
条目,那么您可以这样做

bool allexist = list1.All(l=> list2.Any(x=> x.location == l.market && x.Country == l.Country));
或者,如果您想知道所有
List1
条目都存在于
List2
中,那么

var existsinBoth = list1.Where(l=> list2.Any(x=> x.location == l.market && x.Country == l.Country));

请对你的密码提出疑问。