0

I have a LINQ query with 3 joins and a grouping operation that is throwing a null reference error. In the below query (I'm sorry for the length) if you skip to the 'eDTK_PDP_Description' field, it's that one that it causing the problem. I realize that I have the DefaultIfEmpty specification for the last join but I thought if I assign a value to the field from that join if it's null that should solve the problem. What am I doing wrong here? Thanks!

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}
4

1 回答 1

1

问题是它Result3是 null 并且您正在尝试访问其上的属性。这就是为什么您if实际上并没有解决问题的原因。Result3.SomeField如果为空,则无法检查是否为Result3空,因为尝试的访问基本上会导致,因此在尝试将其用于分配之前NullRefereceException,您需要检查是否为空。Result3如果是,那么您应该使用一些默认值。我会给出一个大致的想法,虽然它不会是 VB,因为我不会写那些废话:P

   if (Result3 != null)
        eDTK_PDP_Description = Result3.SomeField;
   else
        eDTK_PDP_Description = defaultValue;

您必须自己决定如何将此检查放入查询中。Result3整件事对我来说有点多,我不知道在null的情况下你想在那里分配什么。

于 2013-05-24T16:30:09.173 回答