繁体   English   中英

Lin中的.Include()。Where()实体查询

[英].Include() .Where() in Linq to Entities query

我正在尝试找到所有活跃的患者,那些患有EndOfTreatment == null患者。

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

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

在此输入图像描述

我到目前为止的尝试:

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();

我搜索了很多,我得到的最接近的是Moho的回答 ,改编后看起来像:

.SelectMany(p => p.Doctor.TerritoryDoctorPanel)
    .Where(dp => dp.Territory.PromotionalLine == /*p.Product.PromotionalLine*/)
                                                   ^^^^^^^^^^
                                                // How can I reference p.Product here?

恢复:

  • 我需要让Doctors使用在Territories工作的一些Product来治疗Patients 这种关系由Product.PromotionalLine = Territory.PromotionalLine存在。
  • IDProduct的类型是int? ,因此: (IDProduct == null || p.IDProduct == IDProduct.Value)

我真的没有想法如何让它发挥作用。

我感谢任何建议。

我会尝试一些东西,我希望我明白你的想法,虽然我不确定。

所以我想你的问题是这个

我需要让医生使用在地区工作的一些产品来治疗患者。 这种关系存在

这是我将如何做到这一点。

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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM