研究ef 4 c。在join中面对问题。SQL语法

Select a.Code, b.Name from DepartmentMaster a
Join DepartmentDetail  b on isnull( a.ID,0) =isnull( b.ID,0)

注意:a.id和b.id都可以为空
上面的语法需要输入linq语法。下面的语法对我不起作用
Var r=from a in DepartmentMaster
Join b in DepartmentDetail  on a.ID equals b.ID
Select a.Code,b.Name

需要帮助才能在linq ef中编写sql语法isnull()可比进程。
如果有任何疑问,请询问。

最佳答案

如果id为空,则可以使用空合并运算符提供默认值:

from c in DepartmentMaster
join b in DepartmentDetail
    on (a.ID ?? 0) equals (b.ID ?? 0)
select new {
    a.Code, b.Name
}

将生成查询
SELECT [t0].[Code], [t1].[Name] AS [ID1]
FROM [DepartmentMaster] AS [t0]
INNER JOIN [DepartmentDetail] AS [t1]
    ON (COALESCE([t0].[ID],@p0)) = (COALESCE([t1].[ID],@p1))

这与ISNULL不完全相同,但结果应该相同。

09-06 00:24