背景
我有2个表:OrderItem和OrderItemValue。 OrderItem可以有0或1个OrderItemPackageValue。两个表中的主键是相同的,并且有一个从OrderItems到OrderItemPackageValue的外键引用
LINQ
var dummy =
from orderItem in context.OrderItems
select new
{
OrderID = orderItem.OrderID,
LineNumber = orderItem.LineNumber,
PackageValue = (decimal?)orderItem.OrderItemPackageValue.PackageValue
};
var dummy2 = dummy.ToArray();
SQL
SELECT
[Extent1].[OrderID] AS [OrderID],
[Extent1].[LineNumber] AS [LineNumber],
[Extent3].[PackageValue] AS [PackageValue]
FROM [dbo].[OrderItems] AS [Extent1]
LEFT OUTER JOIN [dbo].[OrderItemPackageValues] AS [Extent2] ON ([Extent1].[OrderID] = [Extent2].[OrderID]) AND ([Extent1].[LineNumber] = [Extent2].[LineNumber])
LEFT OUTER JOIN [dbo].[OrderItemPackageValues] AS [Extent3] ON ([Extent2].[OrderID] = [Extent3].[OrderID]) AND ([Extent2].[LineNumber] = [Extent3].[LineNumber])
我试图通过Linq2Entities(MSSQL)获得这个,它似乎正在生成一个额外的外连接,我无法弄清楚如何摆脱它。