Why is Linq to Entities generating Left Outer Joins?

I was wondering if anyone knows why linq for entities always generates left outer joins. I would understand this from optional relationships, but it doesn't make sense when the relationship is required.

Does anyone know how to get it to generate internal joins instead?

+2


a source to share


2 answers


+1


a source


You can create internal connections. You just need to use a keyword join

. For example, here's 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 };

      

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 invoked the LINQ query:

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


a source







All Articles