Linq to Entities中的"Select NOT IN"子句

19

在实体框架中,是否有一种使用“NOT IN(select XXX ...)”子句的方法?
我发现所有的问题都是关于对象列表(IN(1,2,3))的,但我想生成以下语法的查询:

select * from table1 where field1 not in (select subfield from subtable)  

请注意,这是针对实体框架(Linq to Entities)而非SQL的Linq查询语句。

这可行吗?

谢谢!

1个回答

38

像这样:

from c in db.Customers
where !db.Products.Any(p => p.ProductID == c.ProductID)
select c;

终于!一个非常好的答案,甚至没有使用EF4!谢谢! - thomasb
@SLaks:在方法语法中的等效写法是什么? - FMFF
5
@FMFF:db.Customers.Where(c => !db.Products.Any(...)) - SLaks

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接