Dr.*_*len 11 .net c# linq linq-to-objects .net-3.5
我在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
};
Run Code Online (Sandbox Code Playgroud)
错误:
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,
Run Code Online (Sandbox Code Playgroud)
请帮我.
Jon*_*eet 15
cp.DefaultIfEmpty()如果cp为空,则返回一个具有单个空值的序列.
这意味着你必须考虑到一个事实,即p在
from p in cp.DefaultIfEmpty()
Run Code Online (Sandbox Code Playgroud)
可能是null.现在,你还没有真正说出你想要发生的事情.你可能想要这样的东西:
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
};
Run Code Online (Sandbox Code Playgroud)
......或者你可能想要一些不同的处理方式.我们不知道p.product_idor 的类型p.value,哪些没有帮助.(例如,如果product_id是值类型,则需要对上面的代码进行更多的工作.)
你正在进行左联,所以p可以null.你需要考虑到这一点.
这是应该工作的查询,虽然我不确定什么样的价值p.value.如果value是引用类型,则查询将起作用.如果value是值类型,则使用类似于强制转换的product_id强制转换.
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,
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8084 次 |
| 最近记录: |