开发者

Why does Linq to Entities generate Left Outer Joins?

I was wondering if anyone knew why linq to entities always seems to generate left outer joins. I would unders开发者_Python百科tand it on an optional relationship but it doesn't make good sense when the relationship is mandatory.

Does anyone have any idea how to make it generate inner joins instead?


http://blogs.microsoft.co.il/blogs/idof/archive/2008/10/01/entity-framework-generated-queries-look-out.aspx


You can create inner joins. You simply need to use the join keyword. For example, here is a query against the AdventureWorks database:

var query = from od in db.SalesOrderHeader
            join c in db.Customer on od.Customer.CustomerID equals c.CustomerID
            select new { c.AccountNumber, od.OrderDate };

The equivalent SQL:

Select C.AccountNumber, OD.OrderDate 
From Sales.SalesOrderHeader As OD
    Join Sales.Customer As C
        On C.CustomerID = OD.CustomerID

And here is the SQL that the LINQ query produced:

SELECT 
1 AS [C1], 
[Extent2].[AccountNumber] AS [AccountNumber], 
[Extent1].[SalesOrderID] AS [SalesOrderID], 
[Extent1].[RevisionNumber] AS [RevisionNumber], 
[Extent1].[OrderDate] AS [OrderDate], 
[Extent1].[DueDate] AS [DueDate], 
[Extent1].[ShipDate] AS [ShipDate], 
[Extent1].[Status] AS [Status], 
[Extent1].[OnlineOrderFlag] AS [OnlineOrderFlag], 
[Extent1].[SalesOrderNumber] AS [SalesOrderNumber], 
[Extent1].[PurchaseOrderNumber] AS [PurchaseOrderNumber], 
[Extent1].[AccountNumber] AS [AccountNumber1], 
[Extent1].[CreditCardApprovalCode] AS [CreditCardApprovalCode], 
[Extent1].[SubTotal] AS [SubTotal], 
[Extent1].[TaxAmt] AS [TaxAmt], 
[Extent1].[Freight] AS [Freight], 
[Extent1].[TotalDue] AS [TotalDue], 
[Extent1].[Comment] AS [Comment], 
[Extent1].[rowguid] AS [rowguid], 
[Extent1].[ModifiedDate] AS [ModifiedDate], 
[Extent1].[BillToAddressID] AS [BillToAddressID], 
[Extent1].[ContactID] AS [ContactID], 
[Extent1].[ShipMethodID] AS [ShipMethodID], 
[Extent1].[CreditCardID] AS [CreditCardID], 
[Extent1].[CurrencyRateID] AS [CurrencyRateID], 
[Extent1].[CustomerID] AS [CustomerID], 
[Extent1].[SalesPersonID] AS [SalesPersonID], 
[Extent1].[TerritoryID] AS [TerritoryID]
FROM  [Sales].[SalesOrderHeader] AS [Extent1]
INNER JOIN [Sales].[Customer] AS [Extent2] ON ([Extent1].[CustomerID] = [Extent2].[CustomerID]) OR (([Extent1].[CustomerID] IS NULL) AND ([Extent2].[CustomerID] IS NULL))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜