在linq上获取“无法将空值赋给int类型”错误到sql left join

时间:2014-12-16 18:01:25

标签: c# linq

以下是代码:

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();

此行引发错误。提前致谢

1 个答案:

答案 0 :(得分:2)

当没有记录匹配时,aGroup.DefaultIfEmpty()会给null,您需要在使用时进行检查。在将值分配给problemId时,请检查null。
试试这个: -

from aa in aGroup.DefaultIfEmpty()
           select new
           {
               aui2 = relations.AUI2,
               name = conso.STR,
               problemId = aa != null ? aa.ProblemId : 0
           }).ToList();