我已经编写了以下查询来加入几个表。la.UserProfileId 由于某些不正当的原因可以为空。
当我编写等效的 SQL 语句时,此时我应该得到 0 条记录。
var result = (from e in _ctx.Employees
join la in _ctx.LoginAudits on e.UserProfile.Id equals la.UserProfileId.Value
where la.LoginDate >= fromDate
&& e.Client.Id == clientID
select new
{
la.Id,
employeeID = e.Id,
e.Client.DisplayName,
la.UserProfileId
}).ToList();
上面的 LINQ 代码生成下面的 SQL。
exec sp_executesql N'SELECT
1 AS [C1],
[Extent2].[Id] AS [Id],
[Extent1].[Id] AS [Id1],
[Extent3].[DisplayName] AS [DisplayName],
[Extent2].[UserProfileId] AS [UserProfileId]
FROM [dbo].[Employees] AS [Extent1]
INNER JOIN [dbo].[LoginAudits] AS [Extent2] ON ([Extent1].[UserProfile_Id] = [Extent2].[UserProfileId]) OR (([Extent1].[UserProfile_Id] IS NULL) AND ([Extent2].[UserProfileId] IS NULL))
INNER JOIN [dbo].[Clients] AS [Extent3] ON [Extent1].[Client_Id] = [Extent3].[Id]
WHERE ([Extent2].[LoginDate] >= @p__linq__0) AND ([Extent1].[Client_Id] = @p__linq__1)',N'@p__linq__0 datetime2(7),@p__linq__1 bigint',@p__linq__0='2018-02-09 11:11:29.1047249',@p__linq__1=37
如您所见,它包括“OR (([Extent1].[UserProfile_Id] IS NULL) AND ([Extent2].[UserProfileId] IS NULL))”
这与我想要的完全相反。我如何让它做一个正常的内部连接而不是尝试允许空值?
我能够通过在我的 WHERE 子句中添加 && la.UserProfileId != null 来解决这个问题,但理想情况下,我宁愿让 JOIN 表现得像正常的 INNER JOIN 一样,而不是试图预测我不要求的东西。
相关分类