我试图找到所有活跃的患者,那些有 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();
我搜索了很多,我得到的最接近的是 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)
我真的不知道如何让它发挥作用。
我很感激任何建议。
最佳答案
我会尝试一些东西,我希望我能清楚地了解你的想法,尽管我不确定我是否做到了。
所以我想你的问题是这个
I need to get Patients treated by Doctors using some Product working in Territories. The relationship exists
这是我将如何做到的。
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()
关于c# - .Include() .Where() 在 Linq to Entities 查询中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45948965/