.Include() .Where() 在 Linq to Entities 查询中
.Include() .Where() in Linq to Entities query
我正在尝试找到所有活跃的患者,那些 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?
简历:
- 我需要让
Doctors
使用一些 Product
在 Territories
工作来治疗 Patients
。 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()
我正在尝试找到所有活跃的患者,那些 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?
简历:
- 我需要让
Doctors
使用一些Product
在Territories
工作来治疗Patients
。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()