【问题标题】:How do I search the collection of a collection in my LINQ Where clause?如何在我的 LINQ Where 子句中搜索集合的集合?
【发布时间】:2009-04-15 15:16:39
【问题描述】:

我有以下 ADO.NET Entity Framework 实体数据模型:

我想找到所有具有给定 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 语法来做我想做的事?

【问题讨论】:

    标签: .net vb.net linq entity-framework lambda


    【解决方案1】:
    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
    

    【讨论】:

    • 函数“Any”是我需要的线索。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 2010-12-18
    • 2010-09-24
    相关资源
    最近更新 更多