LINQ-嵌套的where子句 [英] LINQ - nested where clause
本文介绍了LINQ-嵌套的where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个项目问题.我正在尝试获取公司列表,但仅过滤位于斯德哥尔摩"的公司.
I have a problem with a project. I’m trying to get a list of companies, but only filter those companies that are located in "Stockholm".
表结构
**Company**:
CompanyID
CompanyName
etc…
**CompanyAddressDetails** (relation table):
Company_CompanyID
CorrespondingAddress_AddressID
**CorrespondingAddress**:
AddressID
StreetName
City
etc…
现在我首先要做的是查询:
Now what I first do is a query:
var companyModel = from c in db.Company select c;
哪个会获得公司的完整列表并具有其对应的地址(可以是多个),所以结果如下:
Which gets the full list of companies and having their Corresponding Addresses (which can be multiple), so the results looks like this:
所以我的问题是:如何根据CorrespondingAddress下的嵌套元素之一进行过滤?例如城市?
So my question is: how can I filter depending on what one of the nestled elements under CorrespondingAddress is? City for example?
到目前为止,我尝试过
companyModel = companyModel.Where(s => s.CorrespondingAddress.Where(x => x.City.Equals("Stockholm")));
companyModel = companyModel.Where(s => s.CorrespondingAddress.ToList().First().Address.Equals("Stockholm"));
但是它们都不起作用.谢谢!
But none of them works. Thanks!
推荐答案
companyModel = companyModel
.Where(s => s.CorrespondingAddress
.Any(x => x.City.Equals("Stockholm")));
这篇关于LINQ-嵌套的where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文