0

我收到以下 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 并分配值的情况下,如何防止出现此错误?

4

1 回答 1

1

由于您实际上是在进行左连接(通过使用),因此fromDefaultIfEmpty的某些实例可能为空。因此,在该子句中,您不能尝试通过调用来读取属性,因为它可能为空。尝试在新对象的实例化中使用一个小语句:PLIh.DefaultIfEmptySelectPLIPLI.Siebel_PDP_DescriptionIf

Select Siebel_PDP_Description = If(PLI Is Nothing, Nothing, PLI.Siebel_PDP_Description)
于 2013-05-22T19:21:33.883 回答