LINQ 左连接错误

2024-01-08

我在 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 
          };

Error:

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,

请帮我。


cp.DefaultIfEmpty()返回一个序列,该序列在 if 中将具有单个空值cp是空的。

这意味着您必须考虑以下事实:p in

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_id or p.value,这没有帮助。 (例如,如果以下情况,您将需要对上面的代码进行更多处理product_id是一个值类型。)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

LINQ 左连接错误 的相关文章

随机推荐