简体   繁体   English

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

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

I'm trying to find all active patients, those with EndOfTreatment == null . 我正在尝试找到所有活跃的患者,那些患有EndOfTreatment == null患者。

The problem is that the relationship has a complex structure. 问题是这种关系结构复杂。

I'm not so good with these database diagram things, but I've made the following pictue, I think you will get the point: 我对这些数据库图表的东西不太满意,但我已经做了以下图片,我想你会明白这一点:

在此输入图像描述

My attempt so far: 我到目前为止的尝试:

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

I searched a lot and the closest I got was this answer from Moho , which adapted would look like: 我搜索了很多,我得到的最接近的是Moho的回答 ,改编后看起来像:

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

Resume: 恢复:

  • I need to get Patients treated by Doctors using some Product working in Territories . 我需要让Doctors使用在Territories工作的一些Product来治疗Patients The relationship exists by Product.PromotionalLine = Territory.PromotionalLine . 这种关系由Product.PromotionalLine = Territory.PromotionalLine存在。
  • IDProduct is of type int? IDProduct的类型是int? , thus: (IDProduct == null || p.IDProduct == IDProduct.Value) ,因此: (IDProduct == null || p.IDProduct == IDProduct.Value)

I'm really out of ideas how to make it work. 我真的没有想法如何让它发挥作用。

I appreciate any suggestion. 我感谢任何建议。

I will try something, I hope I got your idea clearly though I am not sure I did. 我会尝试一些东西,我希望我明白你的想法,虽然我不确定。

so I guess your question is this 所以我想你的问题是这个

I need to get Patients treated by Doctors using some Product working in Territories. 我需要让医生使用在地区工作的一些产品来治疗患者。 The relationship exists 这种关系存在

and here is how i would do it. 这是我将如何做到这一点。

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