在多个条件下加入
本文关键字:条件下 | 更新日期: 2023-09-27 18:05:09
我使用linq-to-sql在数据库中的处方表和名为PatientList的患者列表之间创建连接。
假设表和列表包含一个名为PatientID的int,我将使用它来创建连接,以便根据过去的处方状态过滤患者列表。
我对where子句有一个挑战。处方状态的取值范围是1 ~ 6。每个病人可以有许多不同的处方。我希望从PatientList中删除那些有特定状态的处方的患者。我想要所有至少有过一次状态5的处方的病人,但没有状态4和6,而状态1 2 3是可以的。例如处方a) 3 1 5 3 2或b) 3 5 5 1 3是可以的但c) 2 1 5 6 2或d) 1 3 4 2 1不可以因为第一个含有6第二个没有5
这是我目前为止写的:
var TheOutput = from patients in PatientList
join prescrip in MyDataContext.Prescriptions on
patients.PatientID equals prescrip.PatientID
where prescrip.PrescripStatus == 5 &&
我被困住了,因为如果我这样做的话,情况c)就会变好。
感谢您对这个查询问题的建议。
所以,您想要的是所有得到5分的患者,而不是4或6分的患者。
我不确定是否需要连接。你只是想让病人回来,对吧?
我会尝试这样做:
var TheOutput = (from patients in PatientList
where (from prescrips in MyDataContext.Prescriptions
where prescrips.PatientID = patients.PatientID
&& prescrips.PrescripStatus == 5
select prescrips).Any()
&&!(from prescrips in MyDataContext.Prescriptions
where prescrips.PatientID = patients.PatientID
&& (prescrips.PrescripStatus == 4 || prescrips.PrescripStatus == 6)
select prescrips).Any()
select patients);
试试这样
var TheOutput = from patients in PatientList
join prescrip in MyDataContext.Prescriptions on
patients.PatientID equals prescrip.PatientID
join patients2 in PatientList on
patients.PatientID equals patients2.PatientID
join prescrip2 in MyDataContext.Prescriptions on
patients2.PatientID equals prescrip2.PatientID
where (prescrip.PrescripStatus == 5 && (prescrip2.PrescripStatus != 4 && prescrip2.PrescripStatus != 6))