我的项目有问题。我想获得一份公司名单,但只过滤那些位于“斯德哥尔摩”的公司。
表格结构
**Company**:
CompanyID
CompanyName
etc…
**CompanyAddressDetails** (relation table):
Company_CompanyID
CorrespondingAddress_AddressID
**CorrespondingAddress**:
AddressID
StreetName
City
etc…
现在我首先要做的是查询:
var companyModel = from c in db.Company select c;
它获取完整的公司列表并拥有相应的地址(可以是多个),因此结果如下所示:
所以我的问题是:我如何根据CorrespondingAddress下的一个嵌套元素进行过滤?比如城市?
到目前为止,我试过
companyModel = companyModel.Where(s => s.CorrespondingAddress.Where(x => x.City.Equals("Stockholm")));
companyModel = companyModel.Where(s => s.CorrespondingAddress.ToList().First().Address.Equals("Stockholm"));
但它们都不起作用。谢谢!
答案 0 :(得分:43)
companyModel = companyModel
.Where(s => s.CorrespondingAddress
.Any(x => x.City.Equals("Stockholm")));
答案 1 :(得分:6)
尝试Any()
companyModel = companyModel.Where(s => s.CorrespondingAddress.Any(x => x.City.Equals("Stockholm")));