[英]How to check if a name is contained in another collection
I am doing something like this currently: 我目前正在做这样的事情:
allSales.Where(x => x.location == "NorthAmerica" && x.CompanyName);
I have a List<Company>
and the Company object has a .CompanyName property. 我有一个List<Company>
,并且Company对象具有.CompanyName属性。
I want to filter for sales that have a CompanyName that are in the List collection. 我想过滤具有List集合中CompanyName的销售。
The Company class has the following properties: Company类具有以下属性:
Company
Id
Name
The below doesn't work but it is what I want: 以下不起作用,但这是我想要的:
allSales.Where(x => x.location == "NorthAmerica" &&
companies.Exists(x => x.Name = x.CompanyName));
where companies is List 公司在哪里
Assuming you have a List<Company>
in a variable companies
, you can use LINQ Any
method to do something like this: 假设您在可变companies
具有List<Company>
,则可以使用LINQ Any
方法执行以下操作:
allSales
.Where(x =>
x.location == "NorthAmerica" &&
companies.Any(c => c.Name == x.CompanyName));
You can use the Any()
method on your List<Company>
object with a query to return true or false to your predicate. 您可以对查询使用List<Company>
对象上的Any()
方法,以对谓词返回true或false。 Try something like: 尝试类似:
allSales.Where(x => x.location == "NorthAmerica" && companies.Any(c => c.Name == x.CompanyName);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.