如何将此SQL语句转换为LINQ? [英] How to convert this SQL statement to LINQ?

查看:65
本文介绍了如何将此SQL语句转换为LINQ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何将该SQL语句转换为使用OUTER APPLY和TOP的LINQ.有人可以提出一个想法如何处理它.谢谢!

I have no idea how to convert this SQL statement to LINQ that uses OUTER APPLY and TOP. Can somebody give an idea how deal with it. Thanks!

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

推荐答案

LINQ本机不支持SQL样式的外部联接(在结果集的水平添加中),但是您可以通过进行分组联接来非常轻松地模拟它们如果联接没有结果,则返回一个空组.看看MSDN页面如何:执行左外部联接(C#编程指南)如何:通过使用联接将数据与LINQ结合(可视化基本).

LINQ does not support SQL-style outer joins natively (in the sense of horizontal addition of resultsets), but you can simulate them very easily by doing a grouped join and returning an empty group if the join finds no results. Have a look at the MSDN pages How to: Perform Left Outer Joins (C# Programming Guide) and How to: Combine Data with LINQ by Using Joins (Visual Basic).

这篇关于如何将此SQL语句转换为LINQ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆