如何将此SQL语句转换为LINQ?

时间:2009-10-26 07:12:45

标签: sql linq

我不知道如何将此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

1 个答案:

答案 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)