任何人都可以告诉我如何编写嵌套的SQL查询,如
SELECT*FROM X WHERE X.ID IN(从Y WHERE中选择Y.XID .....)
在LINQ?
你可以尝试:
var yIds = from y in dataContext.Y where ... select y.XId; var query = from x in dataContext.X where yIds.Contains(x.Id) select x;
我不知道它是否会起作用 - 你不想只是做一个连接的原因吗?例如:
var query = from x in dataContext.X join y in dataContext.Y.Where(...) on x.Id equals y.Xid select x;
要在sql中执行IN,您需要使用Linq中的Contains函数.
例如:
var query = from x in GetX() where (from y in GetY() select y.xID).Contains(x.xID) select x;
如果你愿意的话,你也可以单独定义内部linq查询,这样更具可读性
我一直在寻找NOT IN
LINQ to SQL 的解决方案.感谢这个问题,我能够google正确的事情并找到这篇博文:LINQ to SQL中的NOT IN子句
C#
NorthwindDataContext dc = new NorthwindDataContext(); var query = from c in dc.Customers where !(from o in dc.Orders select o.CustomerID) .Contains(c.CustomerID) select c;
VB.net
Dim db As New NorthwinDataContext() Dim query = From c In dc.Customers _ Where Not (From o in dc.Orders _ Select o.CustomerID).Contains(c.CustomerID) _ Select c