2011-08-11 5 views
1

저는 linq-to-sql을 사용하여 db에있는 처방 목록과 환자 호출 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,

var TheOutput = from patients in PatientList 
       join prescrip in MyDataContext.Prescriptions on 
       patients.PatientID equals prescrip.PatientID 
       where prescrip.PrescripStatus == 5 && 

I 때문에 붙어 : 첫 번째는 (6)를 포함하고 두 번째 이것은 내가 지금까지 무엇을하는 5

이 없기 때문에 4,2,1은 확인되지 않습니다 만약 내가 그런 일을한다면, 나는 사건을 일으킬 것이다.

이 쿼리 문제에 대한 제안을 보내 주셔서 감사합니다.

답변

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)) 
같은 시도 :

나는 이런 식으로 뭔가를 시도 할 것이다