1

下面的代码为每个 InvNo 带来一行,以及“SCharge”的总和:0

var Values = from data in DtSet.Tables["tblCosts"].AsEnumerable()
            group data by new
            {
                InvNo = data.Field<double>("InvoiceNo"),
                AccRef = data.Field<double>("SiteRefNum"),
            }
            into g
            select new
            {
                Code = "1",
                InvType = "I",
                Account = g.Key.AccRef,
                InvNo = g.Key.InvNo,
                SCharge = g.Sum(d => d.Field<double>("SCharge"))
            };

有没有办法修改它,以便我在下面得到另一组行,这次对“TCharge”列进行求和,还是我需要做一个新的查询?

4

2 回答 2

2

因此,由于您想显示所有行两次,但第二次投影略有不同,我们可以首先保存分组查询,然后对这些组执行两次投影并将它们组合在一起。

var groupings = DtSet.Tables["tblCosts"].AsEnumerable()
                .GroupBy(data => new
                {
                    InvNo = data.Field<double>("InvoiceNo"),
                    AccRef = data.Field<double>("SiteRefNum"),
                }).ToList();

var Values = groupings.Select(g => new
{
    Code = "1",
    InvType = "I",
    Account = g.Key.AccRef,
    InvNo = g.Key.InvNo,
    Charge = g.Sum(d => d.Field<double>("SCharge"))
})
.Concat(groupings.Select(g => new
{
    Code = "1",
    InvType = "I",
    Account = g.Key.AccRef,
    InvNo = g.Key.InvNo,
    Charge = g.Sum(d => d.Field<double>("SCharge"))
}));
于 2013-01-23T17:25:18.160 回答
0
select new
     {
       Code = "1",
        InvType = "I",
        Account = g.Key.AccRef,
        InvNo = g.Key.InvNo,
        SCharge = g.Sum(d => d.Field<double>("SCharge")),
        TCharge = g.Sum(d => d.Field<double>("TCharge"))
     };
于 2013-01-23T17:27:35.803 回答