简体   繁体   English

如何检查另一个集合中是否包含名称

[英]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.

相关问题 如何打印集合中包含的变量的名称? - how can i print the name of the variables contained in the collection? 如何检查一个列表的值是否包含在另一个列表中? - How to check if a List's value is contained within another List? 如何检查一个集合是否只包含来自另一个集合的元素? - How to check if one collection only contains elements from another collection? 检查列表对象是否包含在另一个列表中 - Check if list object is contained within another list 如何通过反射获取集合中包含的类型 - How to get the type contained in a collection through reflection 如何绑定到其他项目/名称空间中包含的集合? - How to bind to a collection contained in an other project/namespace? 如何检查列表的一个元素值是否包含在 C# LINQ 中的另一个列表中 - How to check one of the element value of list is contained in another list in C# LINQ 如何检查文本中是否包含“不允许”的单词或符号? - How to check if a “not allowed” word or symbol is contained in text? 如何检查带有LINQ to Entities的另一个集合中是否存在实体的子集合的项目? - How can I check whether an item of an Entity's child collection exists in another collection with LINQ to Entities? 表达式树:遍历字符串并检查它们是否包含在另一个表达式中 - Expression Tree: iterate through strings and check if they contained in another Expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM