如果预算没有任何类别,我有以下linq查询抛出错误。难道我做错了什么?如果没有类别,我可以设置总和返回0吗?我对linq来说是个新手。
var r = from rec in DbContext.budgets
where rec.budgetID == updatedBudget.budgetID
select new
{
rec.budgetID,
rec.totalIncome,
totalSpent = rec.categories.Sum(a => a.amount)
};
return new JsonResult(r.FirstOrDefault(), JsonSettings);
答案 0 :(得分:0)
你可以试试这个。
var r = from rec in DbContext.budgets
where rec.budgetID == updatedBudget.budgetID
select new
{
rec.budgetID,
rec.totalIncome,
totalSpent = rec.categories != null ? rec.categories.Sum(a => a.amount) : 0
};
return new JsonResult(r.FirstOrDefault(), JsonSettings);