在EF 4 C上工作#Face加入问题。 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语法输出上面的语法.Bellow语法对我不起作用
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()可比较的过程如果有任何疑问,请询问。
答案 0 :(得分:2)
这可能会对你有所帮助
Var r=from a in DepartmentMaster
Join b in DepartmentDetail on a.(x => (int?)a.ID) ?? 0 equals b.(y => (int?)b.ID) ?? 0
Select a.Code,b.Name
答案 1 :(得分:2)
如果id为null,则可以使用null-coalescing运算符来提供默认值:
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
不完全相同,但结果应该相同。
答案 2 :(得分:1)
您可以使用DefaultIfEmpty
,它将一个空集合替换为一个默认值的集合。因为int的默认值是0
var r=from a in DepartmentMaster
join b in DepartmentDetail on a.ID.DefaultIfEmpty() equals b.ID.DefaultIfEmpty()
Select a.Code,b.Name
答案 3 :(得分:0)
你应该可以这样做:
Join b in DepartmentDetail on (a.ID == null ? 0 : a.ID) equals (b.ID == null ? 0 : b.ID)