2

我试图从以下 SQL Server 2008 R2 查询中查找:

客户及其在订单明细表中放置了某些产品的订单。

此产品必须从交叉引用表中匹配。

该查询还在查看另一个表(激活),如果在其中找到任何订单 ID,则忽略它们。

这是我的查询:

Select O.CustomerId, O.OrderId
from Orders O 
    inner join Customers C
        on O.CustomerId=C.Customerid
where O.orderid not in
(
    Select orderid 
    from activations
)
and OrderId In
(
    Select orderid 
    from  Orderdetails
    WHERE EXISTS
    (
        select * 
        from  OrderActivationCrossRef
        Where Orderdetails.productId=OrderActivationCrossRef.productId
    )
)

可以改进此查询吗? LINQ 的等价物是什么?
先感谢您。

4

2 回答 2

1

确切的变量名称将取决于您的上下文是如何设置的,但您的整体 LINQ 查询应该是这样的:

from o in ctx.Orders 
from c in o.Customers
where !o.Activations.Any() &&
    o.Orderdetails.Any(d => d.OrderActivationCrossRef.Any())
select new {o.CustomerId, o.OrderId}
于 2013-04-09T15:07:27.340 回答
0

这是sql

Select O.CustomerId, O.OrderId
from Orders O 
    inner join Customers C on O.CustomerId=C.Customerid -- forces items that exist in customers
    left join activations a on a.orderid = O.orderid
    join orderdetails d on d.orderid = O.orderid  -- forces only items that exist in details
    join OrderActivationCrossRef c on d.productId = c.productId  -- forces only items that exist in crossref
where 
  a.orderid is null -- forces items that don't exist in activations
于 2013-04-09T16:57:04.007 回答