我不知道如何将此SQL语句转换为使用OUTER APPLY和TOP的LINQ。有人可以说出如何处理它。谢谢!
SELECT Cust.CustomerName, Ord.OnlineOrderTitle, Pro.ProductTitle,
Pic.PictureFilename, PCom.PictureCommentText, Ord.OnlineOrderDateAdded
FROM Customer as Cust
OUTER APPLY
(SELECT * FROM OnlineOrder
WHERE CustomerID = Cust.CustomerID) as Ord
OUTER APPLY
(SELECT * FROM Product
WHERE OnlineOrderID = Ord.OnlineOrderID) as Pro
OUTER APPLY
(SELECT TOP 1 * FROM Accessory
WHERE ProductID = Pro.ProductID) as Acc
OUTER APPLY
(SELECT TOP 1 * FROM Picture
WHERE ProductID = Pro.ProductID) as Pic
OUTER APPLY
(SELECT TOP 1 * FROM PictureComment
WHERE PictureID = Pic.PictureID) as PCom
ORDER BY Ord.OnlineOrderDateAdded DESC
答案 0 :(得分:1)
LINQ本身不支持SQL样式的外连接(在结果集的水平添加的意义上),但是如果连接找不到结果,则可以通过执行分组连接并返回空组来非常容易地模拟它们。请查看MSDN网页How to: Perform Left Outer Joins (C# Programming Guide)和How to: Combine Data with LINQ by Using Joins (Visual Basic)。