有谁能告诉我为什么生成的SQL不包含第二个INNER JOIN?它似乎已被替换为WHERE子句中的NULL检查?我不清楚为什么第二个INNER JOIN不在生成的SQL中。
C#代码:
var cycleList = from o in entities.Orders
join c in entities.Cycles on o.Id equals c.OrderId
join calendar in entities.Calendars on c.CalendarId equals calendar.Id
where o.UnitId == unitId && o.CompanyId == companyId
select c.Id;
生成的SQL:
SELECT
[Extent2].[Id] AS [Id]
FROM [dbo].[Orders] AS [Extent1]
INNER JOIN [dbo].[Cycles] AS [Extent2] ON [Extent1].[Id] = [Extent2].[OrderId]
WHERE ([Extent2].[CalendarId] IS NOT NULL) AND ( CAST( [Extent1].[UnitId] AS int) = @p__linq__0) AND ( CAST( [Extent1].[CompanyId] AS int) = @p__linq__1)
答案 0 :(得分:1)
看起来查询生成器正在优化您的查询。
由于您未在查询中选择或在where子句中使用日历表中的任何字段,因此之间只会进行一次连接订单表和周期表。检查非 NULL 外键的速度可能比连接不使用任何字段的表更快。