我试图找到所有活动的患者,那些EndOfTreatment == null
。.Include().Where()in Linq to Entities查询
问题是关系有一个复杂的结构。
我与这些数据库图表的东西不是那么好,但我已经做了以下pictue,我想你会明白了吧:
我试图至今:
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)
我真的出来的想法如何使它发挥作用。
我欣赏任何建议。
您只是将所有这些数据分组,或者您是否有实际的产品ID或地区ID或两者都用于获取确切的集合? –
@TravisJ。它是一个'JsonResult'并接受'(int?IDProduct = null)',看看我的最后一点。 –
@TravisJ。我明白你的意思。我只是在这种情况下分组,这是一个管理员报告,所以我在这里不使用任何领土ID或医生ID。 –