如何在LINQ Where子句中搜索集合的集合?

Zac*_*son 1 .net linq vb.net lambda entity-framework

我有以下ADO.NET实体框架实体数据模型:

ADO.NET实体数据模型

我想找到具有给定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
Run Code Online (Sandbox Code Playgroud)

Where子句不能以这种方式搜索p.Services和p.Keywords EntityCollections.

'Id'不是'System.Data.Objects.DataClasses.EntityCollection(Of .... Service)'的成员.

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

Dan*_*ner 7

db.PolicyholderSet.Where(ph =>
   ph.Services.Any(s => s.Id == someId) &&
   ph.Keywords.Any(kw => kw.Status == someStatus))
Run Code Online (Sandbox Code Playgroud)

为什么你的查询不起作用?因为p.Services并且p.Keywords是一个Service和一个Keyword集合 - 他们没有财产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
Run Code Online (Sandbox Code Playgroud)