LINQ错误组上的列名称无效

时间:2021-07-16 07:56:31

For some reason, when using two sums on a group by, i run into the error "invalid column name 'id'. When i only do one sum, it works as expected.

出于某种原因,当在组中使用两个总和时,我遇到错误“无效列名'id'。当我只做一个总和时,它按预期工作。

The following fails and throws the error:

以下失败并抛出错误:

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))
    }

However, the following does work fine:

但是,以下工作正常:

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))
    }

And also, when I get the overhead total, instead of the committed total, it works great:

而且,当我获得总开销时,而不是承诺的总数,它的效果很好:

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))
        }

Why can't i get the committed total and overhead total at the same time?

为什么我不能同时获得承诺的总额和间接费用总额?

I have our basic table structure below:

我有我们的基本表结构如下:

PrDetails

Id (int)
PrId (int)
ProgramFund (linq class)
ProjectDetail (linq class)

Id(int)PrId(int)ProgramFund(linq类)ProjectDetail(linq类)

ProjectDetails

Id (int)
CostCenter (linq class)
Wbs (linq class)

Id(int)CostCenter(linq类)Wbs(linq类)

PrAmounts

Id (int)
PrDetailId (int)
CommittedAmount (decimal)
OverheadAmount (decimal)

Id(int)PrDetailId(int)CommittedAmount(decimal)OverheadAmount(decimal)

2 个解决方案

#1


1  

About the only think I can think it might be is a conflict with the names of your parameters. Try this:

关于唯一的想法,我认为它可能是与您的参数的名称冲突。试试这个:

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))
}

#2


3  

You should be starting a SQL profiler and checking the exact SQL being produced to start with and that should head you in the right direction

您应该启动一个SQL分析器并检查正在生成的确切SQL,这应该引导您朝着正确的方向前进

#1


1  

About the only think I can think it might be is a conflict with the names of your parameters. Try this:

关于唯一的想法,我认为它可能是与您的参数的名称冲突。试试这个:

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))
}

#2


3  

You should be starting a SQL profiler and checking the exact SQL being produced to start with and that should head you in the right direction

您应该启动一个SQL分析器并检查正在生成的确切SQL,这应该引导您朝着正确的方向前进