2011-08-11 110 views
1

我正在使用linq-to-sql在db的處方表和患者呼叫PatientList列表之間創建一個連接。加入多個條件

假設表格和列表包含一個名爲PatientID的int,我將使用該int來創建聯接,以按過去處方狀態過濾患者列表。

我對where子句有挑戰。處方的狀態範圍從1到6.每個患者可以有許多不同的處方。我正在從患者列表中刪除已經處於某些狀態的患者。我希望所有至少有一張處方狀態爲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)結果確定。

感謝您對此查詢問題的建議。

回答

0

所以,你要的是有一個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); 
0

嘗試這樣的事情

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))