我有一个带有3个连接的LINQ查询和一个抛出空引用错误的分组操作。在下面的查询中(对不起,我很抱歉)如果你跳到'eDTK_PDP_Description'字段,那就是导致问题的那个。我意识到我有最后一个连接的DefaultIfEmpty规范,但我想如果我从该连接中为该字段分配一个值,如果它为null则应该解决问题。我在这做错了什么?谢谢!
Dim UpdateSiebelPDP1 = From j In ( _
From PDP In SiebelMultPDPDescNoNull _
Group Join Siebel In ProdBase _
On PDP.Siebel_PDP_Code Equals Siebel.Siebel_PDP_Code _
Into g = Group _
From Result In g _
Group Join EDTK In eDTKBase _
On PDP.Siebel_PDP_Code Equals EDTK.eDTK_PDP_Code _
Into h = Group _
From Result2 In h _
Group Join EDTK2 In EDTKPDPOneDescDet _
On PDP.Siebel_PDP_Code Equals EDTK2.PDP_Description _
Into i = Group _
From Result3 In i.DefaultIfEmpty _
Select Result.Siebel_PLI, _
Result.Siebel_PDP_Code, _
Update_DAD_Flag = If(Result.Siebel_DAD_Flag Is Nothing, "Yes", "No"), _
System = "Siebel", _
PDD_PDP_Description = If(Result2.PDD_PDP_Description Is Nothing, _
"", Result2.PDD_PDP_Description), _
Siebel_PDP_Description = "Multiple", _
eDTK_PDP_Description = If(Result3.PDP_Description Is Nothing, _
"Multiple", Result3.PDP_Description)) _
Group j By j.Siebel_PLI, _
j.Siebel_PDP_Code, _
j.Update_DAD_Flag, _
j.System, _
j.PDD_PDP_Description, _
j.Siebel_PDP_Description, _
j.eDTK_PDP_Description _
Into k = Group _
Select New With { _
Siebel_PLI, _
Siebel_PDP_Code, _
Update_DAD_Flag, _
System, _
PDD_PDP_Description, _
Siebel_PDP_Description, _
eDTK_PDP_Description}
答案 0 :(得分:1)
问题是Result3
为空并且您正在尝试访问其上的属性。这就是为什么你的if
实际上没有解决问题的原因。如果Result3.SomeField
为空,则无法检查Result3
是否为空,因为尝试访问会导致NullRefereceException
基本上,您需要先检查Result3
是否为空关于尝试使用它进行分配。如果是,那么您应该使用一些默认值。我会给出一个大致的想法,虽然它不会是VB因为我不写那些废话:P
if (Result3 != null)
eDTK_PDP_Description = Result3.SomeField;
else
eDTK_PDP_Description = defaultValue;
您必须自己决定如何将此检查放入查询中。整个事情对我来说有点多,我不知道在Result3
为空的情况下你想要分配什么。