【问题标题】:Error in LINQ Left JOINLINQ 左连接中的错误
【发布时间】:2011-05-24 06:26:43
【问题描述】:

我在 LINQ 中编写了下面的查询来执行左连接,但它抛出错误:

var qry = from c in dc.category_feature_Name_trans_SelectAll_Active()
          join p in dc.product_category_feature_trans_SelectAll()
          on c.cft_id equals p.cft_id into cp
          from p in cp.DefaultIfEmpty()                      
          select new
          {
              c.cft_id,
              c.feature_id,
              c.feature_name,
              p.product_id ,
              p.value 
          };

错误:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the 
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:

Line 57:                       on c.cft_id equals p.cft_id into cp
Line 58:                       from p in cp.DefaultIfEmpty()                      
error Line 59:                       select new
Line 60:                       {
Line 61:                           c.cft_id,

请帮帮我。

【问题讨论】:

    标签: c# .net linq .net-3.5 linq-to-objects


    【解决方案1】:

    cp.DefaultIfEmpty() 返回一个序列,如果cp 为空,则该序列将具有一个空值。

    这意味着您必须考虑到p

    from p in cp.DefaultIfEmpty()
    

    可能为空。现在,你还没有真正说出在这种情况下你想要发生什么。你可能想要这样的东西:

    var qry = from c in dc.category_feature_Name_trans_SelectAll_Active()
              join p in dc.product_category_feature_trans_SelectAll()
              on c.cft_id equals p.cft_id into cp
              from p in cp.DefaultIfEmpty()                      
              select new
              {
                  c.cft_id,
                  c.feature_id,
                  c.feature_name,
                  product_id = p == null ? null : p.product_id,
                  value = p == null ? null : p.value 
              };
    

    ... 或者您可能需要一些不同的处理方式。我们不知道p.product_idp.value 的类型,这无济于事。 (例如,如果product_id 是值类型,则需要对上述代码进行更多处理。)

    【讨论】:

    • product_id = p == null ? null : p.product_id 不会编译,如果 product_id 是 int
    • @Alex:这就是我写最后一句话的原因...如果product_id 是字符串,您的版本将无法编译:)
    • 没有错误...像往常一样,乔恩总是给出正确的答案和正确的解释。谢谢乔恩。
    【解决方案2】:

    您正在进行左连接,所以p 可以是null。你需要考虑到这一点。

    这里是应该工作的查询,虽然我不确定p.value 是什么类型的值。如果 value 是引用类型,则查询将起作用。如果 value 是 value 类型,则使用类似于product_id cast。

    var qry = from c in dc.category_feature_Name_trans_SelectAll_Active()
                join p in dc.product_category_feature_trans_SelectAll()
                on c.cft_id equals p.cft_id into cp
                from p in cp.DefaultIfEmpty()
                select new
                {
                    c.cft_id,
                    c.feature_id,
                    c.feature_name,
                    product_id = p == null ? (int?)null : p.product_id,
                    value = p == null ? null : p.value,
                };
    

    【讨论】:

      【解决方案3】:

      我在 LINQ 查询中对多个表使用 LEFT JOIN 时遇到了同样的问题,并且我遇到了空引用异常。我使用“?”来检查空值的技巧。如果我的方法不正确,请纠正我。

      var deviceResultDetails = from pa in programAliasrecords
                                join pgm in Newprogramrecords on pa.program_id equals pgm.id into pgm_join
                                from pgm2 in pgm_join.DefaultIfEmpty()
                                join latest_fw in firmwareWithIdrecords on pgm2?.latest_firmware_id equals latest_fw?.id into latest_fw_join
                                from latest_fw2 in latest_fw_join.DefaultIfEmpty()
                                join dev_fw in firmwareWithIdrecords on pgm2?.firmware_group_id equals dev_fw?.firmware_group_id into dev_fw_join
                                from dev_fw2 in dev_fw_join.DefaultIfEmpty()
                                join vv in vulnerabilityrecords on pgm2?.id equals vv?.program_id into vv_join
                                from vv2 in vv_join.DefaultIfEmpty()
                                where 
                                dev_fw2?.version == row.firmware    
                                && pa?.keyword == row.model      
      
                                   select new _deviceResults
                                   {
                                       model = row.model, 
                                       serial = row.serial, 
                                       firmware = row.firmware, 
                                       firmware_date = dev_fw2.br_date == null ? null: dev_fw2.br_date,
                                       latest_firmware = latest_fw2.version == null ? null : latest_fw2.version,
                                       latest_firmware_date = latest_fw2.br_date == null ? null : latest_fw2.br_date,
      
                                       status = Convert.ToInt32(vulnerability_count) > 0 && pgm2.out_of_support == "TRUE" ? "Vulnerable (End of Suport)" :
                                                Convert.ToInt32(vulnerability_count) > 0 && pgm2.out_of_support == " " ? "Vulnerable (Upgradeable)" :
                                                       pgm2.out_of_support == "TRUE" ? "Out-of-support" :
                                                       Convert.ToInt32(dev_fw2.revs_out_of_date) > 1 ? "Out-of-date" :
                                                       pgm2.id == "NonHP" ? "NonHP" :
                                                       pgm2.id == "NoFirmware" ? "No Firmware" :
                                                       pgm2.id == "HPink" || pgm2?.id == "HPOther" ? "Not evaluated (model not included yet)" :
                                                       pgm2.id == " " ? "Not evaluated (model not recognized)" :
                                                       dev_fw2.version == " " ? "Not evaluated (firmware version missing)" :
                                                       dev_fw2.id == " " ? "Not evaluated (firmware version mismatch)" :    // && dev_fw.id  in (select version from firmware) 
                                                       dev_fw2.id == " " ? "Not evaluated (firmware version unrecognized)" :
                                                       dev_fw2.br_date == " " || pgm2.id == " " || dev_fw2.revs_out_of_date == " " ? "Not evaluated" : "OK",
      
                                       out_of_support = pgm2.out_of_support == "" ? "false" : pgm2.out_of_support,
                                       revs_out_of_date = dev_fw2.revs_out_of_date == null ? null : dev_fw2.revs_out_of_date,
                                       program_id = pgm2.id == null ? null :  pgm2.id,
                                       firmware_id = dev_fw2.id == null ? null : latest_fw2.br_date
                                   };
      

      【讨论】:

        【解决方案4】:

        使用您的模型类作为 DefaultIfEmpty() 函数的参数。

        from p in cp.DefaultIfEmpty(new yourModelClass())
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-04
          • 1970-01-01
          相关资源
          最近更新 更多