在 LINQ to SQL 左联接上收到“无法将 null 值分配给整数类型”错误
本文关键字:null 无法将 分配 错误 类型 整数 SQL to LINQ | 更新日期: 2023-09-27 18:34:21
这是代码:
var kids = (from relations in masterDB.mrrel_Limited2s
join conso in masterDB.mrconso_Limiteds on relations.AUI2 equals conso.AUI
join a in masterDB.tbl_patients_problems_problemId_to_SnoMed_Iteration2_before_doc_final_s on conso.SCUI equals a.SnoMedScui into aGroup
where relations.AUI1.Equals(contextDictionary["CategoryID"].ToString())
&&
relations.REL.Equals("CHD")
from aa in aGroup.DefaultIfEmpty()
select new
{
aui2 = relations.AUI2,
name = conso.STR,
problemId = aa.ProblemId
}).ToList();
此行引发错误。提前致谢
当没有匹配的记录时,您的aGroup.DefaultIfEmpty()
会给出null
,您需要在使用时检查。将值分配给问题 ID 时,请检查空值。
试试这个:-
from aa in aGroup.DefaultIfEmpty()
select new
{
aui2 = relations.AUI2,
name = conso.STR,
problemId = aa != null ? aa.ProblemId : 0
}).ToList();