我有两个目标,让我们给他们打电话Input和Output
Input有属性Input_ID,Label并且有属性和Input_Amt
OutputOutput_IDOutput_Amt
我想在LINQ中执行等效的SQL语句:
SELECT Label, Sum(Added_Amount) as Amount FROM
(SELECT I.Label, I.Input_Amt + ISNULL(O.Output_Amt, 0) as Added_Amount
FROM Input I LEFT OUTER JOIN Output O ON I.Input_ID = O.Output_ID)
GROUP BY Label
Run Code Online (Sandbox Code Playgroud)
对于内部查询,我写的是:
var InnerQuery = from i in input
join o in output
on i.Input_ID equals o.Output_ID into joined
from leftjoin in joined.DefaultIfEmpty()
select new
{
Label = i.Label,
AddedAmount = (i.Input_Amt + leftjoin.Output_Amt)
};
Run Code Online (Sandbox Code Playgroud)
但是,在测试中,语句返回null.是什么赋予了?
另外,在我将所有金额添加到一起后,如何在单个LINQ语句中继续所需的查询并执行组?
好的,既然我明白了什么会好一些,那么主要的问题是你没有得到相当于ISNULL的位.试试这个:
var InnerQuery = from i in input
join o in output
on i.Input_ID equals o.Output_ID into joined
from leftjoin in joined.DefaultIfEmpty()
select new
{
Label = i.Label,
AddedAmount = (i.Input_Amt + (leftjoin == null ? 0 : leftjoin.Output_Amt))
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4057 次 |
| 最近记录: |