LINQ 左连接错误
我在 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,
请帮助我。
i have written below query in LINQ to perform left join but its throwing error:
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,
Please help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
cp.DefaultIfEmpty()
返回一个序列,如果cp
为空,则该序列将具有单个 null 值。事实
这意味着您必须考虑到
p
中的可能为空这一 。现在,你还没有真正说出在这种情况下你想要发生什么。您可能想要这样的东西:...或者您可能想要一些不同的处理。我们不知道
p.product_id
或p.value
的类型,这没有帮助。 (例如,如果product_id
是值类型,则需要对上述代码进行更多处理。)cp.DefaultIfEmpty()
returns a sequence which will have a single null value in ifcp
was empty.That means you have to account for the fact that the
p
inmay be null. Now, you haven't really said what you want to happen in that case. You might want something like this:
... or you may want some different handling. We don't know the types of
p.product_id
orp.value
, which doesn't help. (For example, you'll need a bit more work with the above code ifproduct_id
is a value type.)您正在进行左连接,因此
p
可以为null
。你需要考虑到这一点。这是应该有效的查询,尽管我不确定
p.value
是什么类型的值。如果值是引用类型,则查询将起作用。如果 value 是值类型,则使用类似于product_id
转换的转换。You are making left join, so
p
can benull
. You need to account for that.Here is the query that should work, although I don't know for sure what kind of value is
p.value
. The query will work if value is a reference type. If value is value type, than use the cast similar toproduct_id
cast.在 LINQ 查询中使用 LEFT JOIN 到多个表时,我遇到了同样的问题,并且遇到了空引用异常。我使用了使用“?”来检查空值的技巧。如果我的方法不正确,请纠正我。
I am facing the same issue while using the LEFT JOIN to multiple tables in LINQ query and I was facing null reference exception. I used the trick to check the null value using the "?" Please correct me if my approach is incorrect.
使用模型类作为 DefaultIfEmpty() 函数的参数。
Use your Model Class as a parameter for DefaultIfEmpty() function.