出于某种原因,当在组中使用两个和时,我遇到错误“无效列名'id'。当我只做一个总和时,它按预期工作。
以下失败并抛出错误:
from pd in PrDetails.Where(_pd => _pd.PrId == 46)
group pd by new { pd.ProgramFund, pd.ProjectDetail.CostCenter, pd.ProjectDetail.Wbs }
into g
select new
{
g.Key.ProgramFund,
g.Key.CostCenter,
g.Key.Wbs,
CommittedTotal = g.Sum(_pd => _pd.PrDetailPrAmounts.Sum(_pa => _pa.CommittedAmount)),
OverheadTotal = g.Sum(_pd => _pd.PrDetailPrAmounts.Sum(_pa => _pa.OverheadAmount))
}
但是,以下 可以正常工作:
from pd in PrDetails.Where(_pd => _pd.PrId == 46)
group pd by new { pd.ProgramFund, pd.ProjectDetail.CostCenter, pd.ProjectDetail.Wbs }
into g
select new
{
g.Key.ProgramFund,
g.Key.CostCenter,
g.Key.Wbs,
CommittedTotal = g.Sum(_pd => _pd.PrDetailPrAmounts.Sum(_pa => _pa.CommittedAmount))
}
还,当我获得总开销时,而不是承诺的总数,它的效果很好:
from pd in PrDetails.Where(_pd => _pd.PrId == 46)
group pd by new { pd.ProgramFund, pd.ProjectDetail.CostCenter, pd.ProjectDetail.Wbs }
into g
select new
{
g.Key.ProgramFund,
g.Key.CostCenter,
g.Key.Wbs,
OverheadTotal = g.Sum(_pd => _pd.PrDetailPrAmounts.Sum(_pa => _pa.OverheadAmount))
}
为什么我不能同时获得承诺的总计和开销总额?
我的基本表结构如下:
Id(int)
PrId(int)
ProgramFund(linq类)
ProjectDetail(linq类)
Id(int)
CostCenter(linq类)
Wbs(linq类)
Id(int)
PrDetailId(int)
CommittedAmount(十进制)
OverheadAmount(十进制)
答案 0 :(得分:3)
你应该启动一个SQL分析器并检查正在生成的确切SQL,这应该引导你朝着正确的方向
答案 1 :(得分:1)
关于唯一的想法,我认为它可能与您的参数名称冲突。试试这个:
from pd in PrDetails.Where(_pd => _pd.PrId == 46)
group pd by new { pd.ProgramFund, pd.ProjectDetail.CostCenter, pd.ProjectDetail.Wbs }
into g
select new
{
g.Key.ProgramFund,
g.Key.CostCenter,
g.Key.Wbs,
CommittedTotal = g.Sum(_pd1 => _pd1.PrDetailPrAmounts.Sum(_pa1 => _pa1.CommittedAmount)),
OverheadTotal = g.Sum(_pd2 => _pd2.PrDetailPrAmounts.Sum(_pa2 => _pa2.OverheadAmount))
}