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

时间:2017-08-29 22:03:52

标签: 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?

恢复:

  • 我需要Patients使用在Doctors工作的Product Territories来对待Product.PromotionalLine = Territory.PromotionalLine。这种关系存在于IDProduct
  • int?的类型为(IDProduct == null || p.IDProduct == IDProduct.Value),因此:http or irc or dns

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

我感谢任何建议。

1 个答案:

答案 0 :(得分:3)

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

所以我想你的问题是这个

  

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

以及我将如何做到这一点。

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