Sql不在Linq中的问题[英] Sql Not In in Linq Issue

本文是小编为大家收集整理的关于Sql不在Linq中的问题的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有这个 sql 查询.我可以将大部分查询转换为 linq,但我不明白如何在这个特定查询中实现"不在"部分.这是我的查询:

SELECT d.processid,d.name,t.test_name
FROM dc_tp_tprocess d
left outer join dc_tp_tprocedured pd on pd.processid=d.processid
left outer join dc_tp_test t on t.procedureid=pd.procedureid
where d.active='Y' and t.testid=1 
                   and d.processid not in (1,2,6,8,9,10)
order by t.testid,d.processid

推荐答案

就这样:

int[] tmp=new int[]{1,2,6,8,9,10};

var query=from d in dc_tp_tprocess
          join pd in dc_tp_tprocedured on d.processid equals pd.processid into leftGroup1
          from pd in leftGroup1.DefaultIfEmpty() 
          join t in dc_tp_test on pd.procedureid equals t.procedureid into leftGroup2
          from t in leftGroup2.DefaultIfEmpty()
          orderby t.testid,d.processid
          where d.active=="Y" && t.testid==1 && !tmp.Contains(d.processid)
          select new {d.processid,d.name,test_name=t==null?"":t.test_name};

本文地址:https://www.itbaoku.cn/post/1557186.html

问题描述

I have this sql query. I can convert most of the query to linq but I don't understand how to implement the "not in" portion in this particular query. Here is my query:

SELECT d.processid,d.name,t.test_name
FROM dc_tp_tprocess d
left outer join dc_tp_tprocedured pd on pd.processid=d.processid
left outer join dc_tp_test t on t.procedureid=pd.procedureid
where d.active='Y' and t.testid=1 
                   and d.processid not in (1,2,6,8,9,10)
order by t.testid,d.processid

推荐答案

just like this:

int[] tmp=new int[]{1,2,6,8,9,10};

var query=from d in dc_tp_tprocess
          join pd in dc_tp_tprocedured on d.processid equals pd.processid into leftGroup1
          from pd in leftGroup1.DefaultIfEmpty() 
          join t in dc_tp_test on pd.procedureid equals t.procedureid into leftGroup2
          from t in leftGroup2.DefaultIfEmpty()
          orderby t.testid,d.processid
          where d.active=="Y" && t.testid==1 && !tmp.Contains(d.processid)
          select new {d.processid,d.name,test_name=t==null?"":t.test_name};