C# 如何防止实体框架在使用LINQ连接可为null的属性时添加ISNULL检查

C# 如何防止实体框架在使用LINQ连接可为null的属性时添加ISNULL检查,c#,entity-framework,linq,C#,Entity Framework,Linq,我有一个Linq查询,它连接了2个可为null的属性 var result = (from A in context.TableA join B in context.TableB on A.ExecutionId equals B.ExecutionId where B.InsertedBy == userName rep.ExecutionId和sch.ExecutionId在数据库中都可以为null int

我有一个Linq查询,它连接了2个可为null的属性

 var result = (from A in context.TableA
             join B in context.TableB
             on A.ExecutionId equals B.ExecutionId
             where B.InsertedBy == userName 
rep.ExecutionId和sch.ExecutionId在数据库中都可以为null int。我真正希望实体框架生成的是

select 
column1     
from TableA A
inner join TableB B on A.ExecutionId = B.ExecutionId
where B.InsertedBy = @username
但我得到的是

select 
column1
from TableA A
inner join TableB B on A.ExecutionId = B.ExecutionId 
OR ((A.[ExecutionId] IS NULL) AND (B.[ExecutionId] IS NULL))
where B.InsertedBy = @username

(两个ExecutionID都不是主键)。第二个查询为我提供了比我需要的更多的记录,但更重要的是,不是我想要的查询。如何编写LINQ,使其生成第一个查询或等效查询?

如果您不希望任何
ExecutionID
null
的记录,请使用筛选器

var result = (from A in context.TableA.Where( a => a.ExecutionID != null )
              join B in context.TableB.Where( b => b.ExecutionId != null ) 
              on A.ExecutionId equals B.ExecutionId
              where B.InsertedBy == userName 
              ...`enter code here`

尝试
A.ExecutionId.Value等于B.ExecutionId.Value
Adding.Value不会更改生成的SQL。不过我很欣赏这个建议。你知道C#计算NULL==NULL为true,SQL为false吗?是@SirRufo。我不想返回任何带有null executionid的记录,但我也想加入该列。也许这个答案可以帮助你。谢谢!我不知道你能做到。