2017-08-29 174 views
1

我试图找到所有活动的患者,那些EndOfTreatment == null.Include().Where()in Linq to Entities查询

问题是关系有一个复杂的结构。

我与这些数据库图表的东西不是那么好,但我已经做了以下pictue,我想你会明白了吧:

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使用一些ProductTerritories工作处理。关系存在Product.PromotionalLine = Territory.PromotionalLine
  • IDProductint?类型,即:(IDProduct == null || p.IDProduct == IDProduct.Value)

我真的出来的想法如何使它发挥作用。

我欣赏任何建议。

+0

您只是将所有这些数据分组,或者您是否有实际的产品ID或地区ID或两者都用于获取确切的集合? –

+0

@TravisJ。它是一个'JsonResult'并接受'(int?IDProduct = null)',看看我的最后一点。 –

+0

@TravisJ。我明白你的意思。我只是在这种情况下分组,这是一个管理员报告,所以我在这里不使用任何领土ID或医生ID。 –

回答

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

我改变了包含使用'Territory'这种方式:'.Include(p => p.Doctor.TerritoryDoctorPanel.Select(dp => dp.Territory))'和在部分't =>/*在这里为TerritoryDoctorPanel添加条件* /'我用过:'dp => dp.Territory.PromotionalLine == p.Product.PromotionalLine' ...我不得不说:人,你统治! !它现在像一个魅力工作:) ...谢谢你! ...刚刚恢复,这里的教训是结合'。Where()'用'.Any()'代替'.Include()'用'.Where()'代替。 –

+0

很高兴听到 谢谢,很高兴我帮助:) – Munzer