首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于多个条件进行联接

基于多个条件进行联接
EN

Stack Overflow用户
提问于 2011-08-11 23:57:45
回答 2查看 95关注 0票数 1

我正在使用linq- to -sql在db中的处方表和患者调用PatientList列表之间创建连接。

假设表和列表包含一个名为PatientID的整数,我将使用它创建连接,根据过去的处方状态过滤患者列表。

我对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。

这就是我到目前为止所知道的:

代码语言:javascript
复制
var TheOutput = from patients in PatientList
                join prescrip in MyDataContext.Prescriptions on 
                patients.PatientID equals prescrip.PatientID
                where prescrip.PrescripStatus == 5 && 

我被卡住了,因为如果我做了这样的事情,我会让情况c)变成好的。

感谢您对此查询问题的建议。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-12 00:04:53

因此,您希望所有的患者都有5分,而不是4分或6分。

我不确定是否需要加入。你只是想送回病人,对吧?

我会尝试这样的东西:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2011-08-12 00:10:17

试试这样的东西

代码语言:javascript
复制
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))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7029059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档