How do I convert this SQL statement to LINQ?
I don't know how to convert this SQL statement to LINQ which uses OUTER APPLY and TOP. Can anyone provide an idea how to handle this. Thank!
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
+2
source to share
1 answer
LINQ does not natively support SQL-style outer joins (in the sense of adding result sets horizontally), but you can easily simulate them by doing a grouped join and returning an empty group if the join finds no results. Take a look at the MSDN How To Guide pages . Performing Left Outer Joins (C # Programming Guide) and How to Join Data with LINQ Using Joins (Visual Basic) .
+1
source to share