转[Linq To SQL]生成[Not In]语句

Posted on 2010-12-16 20:47  gmtyt  阅读(409)  评论(0)    收藏  举报
转[Linq To SQL]生成[Not In]语句
2010-03-11 23:24

以前一直觉得linq to sql生成类似 where id not in (1,2,3) 或 where id not in (select id from ...) 这样的条件不是很方便,每次我都是把条件ID事先取到一个数组里,然后用 !Arr.Contains(c.Id)这样处理,其实可以完全直接用linq写成一句,如下:

from a in TableA where !(from b in TableB Where ... select b.Id).Contains(a.Id)

最终翻译出来的语句并非跟not in 完全一样的,而是用not exists(...),不过效果完全相同,能达到要求就行了

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3