我想知道是否有人知道为什么linq to entities似乎总是产生左外连接。我会在一个可选的关系中理解它,但是当关系是强制性的时候它没有意义。
有没有人知道如何让它生成内部联接呢?
答案 0 :(得分:1)
答案 1 :(得分:0)
您可以创建内部联接。您只需使用join
关键字即可。例如,这是针对AdventureWorks数据库的查询:
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 };
等效的SQL:
Select C.AccountNumber, OD.OrderDate
From Sales.SalesOrderHeader As OD
Join Sales.Customer As C
On C.CustomerID = OD.CustomerID
这是LINQ查询生成的SQL:
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))