c# - .Include() .Where() 在 Linq to Entities 查询中

标签 c# entity-framework linq linq-to-entities

我试图找到所有活跃的患者,那些有 EndOfTreatment == null 的患者.

问题是这种关系具有复杂的结构。

我不太擅长这些数据库图表的东西,但我做了以下图片,我想你会明白这一点:

enter image description here

到目前为止我的尝试:

var ans = ctx
    .PatientMap
    .Include(p => p.Doctor)
    .Include(p => p.Product)
    .Include(p => p.Institution)
    .Include(p => p.Doctor.TerritoryDoctorPanel
        .Where(dp =>
            (dp.Territory.PromotionalLine == p.Product.PromotionalLine) &&  // issuing
            (dp.Territory.Active == true)                                   // lines
        )
    )
    .Where(p =>
        (IDProduct == null || p.IDProduct == IDProduct.Value) &&
        (p.EndOfTreatment == null)
    )
    .ToList()
    .Select(p => new ActivePatientModel
    {
        IDPatient = p.ID,
        Observation = p.Observation,
        TreatmentPeriod = DateTimeSpan.CompareDates(
            (DateTime)p.StartOfTreatment, DateTime.Now
        ).Months,
        NameDoctor = p.Doctor.FullName,
        CodeDoctor = p.Doctor.Code,
        CodeInstitution = p.Institution.Code,
    })
    .ToList();

我搜索了很多,我得到的最接近的是 this answer from Moho ,经过调整后看起来像:
.SelectMany(p => p.Doctor.TerritoryDoctorPanel)
    .Where(dp => dp.Territory.PromotionalLine == /*p.Product.PromotionalLine*/)
                                                   ^^^^^^^^^^
                                                // How can I reference p.Product here?

恢复:
  • 我需要获取 PatientsDoctors 处理使用一些 ProductTerritories 工作.关系存在于 Product.PromotionalLine = Territory.PromotionalLine .
  • IDProduct类型为 int? ,因此:(IDProduct == null || p.IDProduct == IDProduct.Value)

  • 我真的不知道如何让它发挥作用。

    我很感激任何建议。

    最佳答案

    我会尝试一些东西,我希望我能清楚地了解你的想法,尽管我不确定我是否做到了。

    所以我想你的问题是这个

    I need to get Patients treated by Doctors using some Product working in Territories. The relationship exists



    这是我将如何做到的。
    var ans = ctx
        .PatientMap
        .Include(p => p.Doctor)
        .Include(p => p.Product)
        .Include(p => p.Institution)
        .Include(p => p.Doctor.TerritoryDoctorPanel
        .Where(p => 
            // some doctors, doctorIDs is list of all doctors id you want in case you are using id retrieval 
           doctorIDs.Contains(p.DoctorID) &&
           //working in some territory 
           //similar to this, you can filter any doctor Attribute 
           p.Doctor.TerritoryDoctorPanel.Any(t => /*Add condition for TerritoryDoctorPanel here */) &&
          (p.IDProduct == null || p.IDProduct == IDProduct.Value) &&
          (p.EndOfTreatment == null) && 
           // Product Promotion line conditions
           // also similar to this you can filter any product attribute 
          (p.Product.PromotionalLine.Any(pl => /*Add condition for promotional lines here*/)))             
        .ToList()
    

    关于c# - .Include() .Where() 在 Linq to Entities 查询中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45948965/

    相关文章:

    c# - 值得学习的开源项目

    c# - 文本框 - 绑定(bind)的属性值不会立即更新

    c# - 批量插入和更新性能 C# "ADO.NET"Vs "Linq2SQL"Vs "EF-DataFirst approach"

    c# - ADO .NET EF 中的可组合查询每次迭代返回更多实体

    c# - 此 XPath 的 LINQ to XML 等效项是什么

    c# - 我可以在 ASP.NET 中使用 C# 获取时间参数的本地化缩写吗?

    c# - 快速创建数千个线程并同时执行它们

    c# - EF5 db.Database.SqlQuery 映射返回的对象

    c# - 是否可以将反射与 linq to entity 一起使用?

    c# - 使用基类 IEqualityComparer 执行 Distinct(),并且仍然返回子类类型?