连接时LINQ null引用异常

时间:2013-05-22 19:08:54

标签: vb.net linq

我得到以下linq查询的空引用异常:

 Dim Report = From Filter In EDTKPDPDescVar _
                         Group Join EDTK In eDTKBase _
                         On Filter.eDTK_PDP_Code Equals EDTK.eDTK_PDP_Code _
                         Into g = Group _
                         From EDTK In g.DefaultIfEmpty _
                         Group Join PLI In ProdBase _
                         On EDTK.eDTK_PLI Equals PLI.Siebel_PLI _
                         Into h = Group _
                         From PLI In h.DefaultIfEmpty _
                         Select eDTK_PDP_Code = Filter.eDTK_PDP_Code, _
                                eDTK_PLI = EDTK.eDTK_PLI, _
                                System = "eDTK Instance: " + EDTK.eDTK_Instance, _
                                PDD_PDP_Description = EDTK.PDD_PDP_Description, _
                                Siebel_PDP_Description = PLI.Siebel_PDP_Description, _
                                eDTK_PDP_Description = EDTK.eDTK_PDP_Description

创建错误的字段(数据类型是字符串)是这样的:

 Siebel_PDP_Description = PLI.Siebel_PDP_Description

如果没有if / then语句测试null并赋值,我怎么能防止弹出这个错误?

1 个答案:

答案 0 :(得分:1)

由于您实际上是在进行左连接(使用DefaultIfEmpty),因此来自PLI的{​​{1}}的某些实例可能为空。因此,在h.DefaultIfEmpty子句中,您无法通过调用Select来尝试从PLI读取属性,因为它可能为空。尝试在新对象的实例化中使用小PLI.Siebel_PDP_Description语句:

If