2009-04-15 88 views
1

我有以下ADO.NET实体框架实体数据模型:如何在LINQ Where子句中搜索集合的集合?

ADO.NET Entity Data Model

我想找到所有的投保人既具有给定id的服务,也给定状态的关键词。

这LINQ不起作用:

Dim ServicesId As Integer = ... 
Dim KeywordStatus As Integer = ... 

Dim FoundPolicyholders = From p As Policyholder In db.PolicyholderSet.Include("Keywords").Include("Services") _ 
         Where p.Services.Id = ServicesId _ 
         And p.Keywords.Status = KeywordStatus _ 
         Select p 

WHERE子句不能搜索那样的p.Services和p.Keywords EntityCollections。

'ID' 是不 'System.Data.Objects.DataClasses.EntityCollection(中 ....服务)' 的成员。

什么是正确的LINQ语法来做我想要的?

回答

6
db.PolicyholderSet.Where(ph => 
    ph.Services.Any(s => s.Id == someId) && 
    ph.Keywords.Any(kw => kw.Status == someStatus)) 

为什么你的查询不起作用?因为p.Servicesp.KeywordsServiceKeyword集合 - 它们没有属性IdStatus因此您不能使用p.Services.Idp.Keywords.Status

Visual Basic…

Dim FoundPolicyholders = From p As Policyholder In db.PolicyholderSet.Include("Keywords").Include("Services") _ 
         Where p.Services.Any(Function(s) s.Id = ServicesId) _ 
         And p.Keywords.Any(Function(k) k.Status = KeywordStatus) _ 
         Select p 
+0

函数“任何”是我需要的线索。谢谢。 – 2009-04-15 15:35:11