如何在一个 LINQ 上获取子值的总和

发布于 2025-01-06 10:42:15 字数 629 浏览 1 评论 0原文

这是我的结构:

程序 - 描述等...

操作 - Program_Id、描述等。

成本 - Action_Id、Value1、Value2、Value3

一个操作可以有多个成本。 我需要的是一个按程序对这些值进行分组的查询。例如:

“程序名称”|总价值1 |总价值 2 |计划总计

这是我迄今为止的努力:

var ListByPrograma = from a in db.Actions
    join c in db.Costs on a.Id equals c.Action_Id
    group a by a.Program into p
    select new 
    {
        Program = p.Key,
        actionsQuantity = p.Count(),
        totalValue1 = p.Costs.????
        totalValue2 = ?,
        totalByProgram = ?
    };

This is the structure I have:

Program
- Description, etc...

Action
- Program_Id, Description, etc..

Cost
- Action_Id, Value1, Value2, Value3

One Action can Have multiple Costs.
What I Need is a query that group this values by Program. Like:

"Program name" | Total of Value1 | Total of Value 2 | Total of the program

This is my effort so far:

var ListByPrograma = from a in db.Actions
    join c in db.Costs on a.Id equals c.Action_Id
    group a by a.Program into p
    select new 
    {
        Program = p.Key,
        actionsQuantity = p.Count(),
        totalValue1 = p.Costs.????
        totalValue2 = ?,
        totalByProgram = ?
    };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱给你人给你 2025-01-13 10:42:15

这样的东西有用吗?

var ListByPrograma  = from a in db.Actions
 join c in db.Costs on a.ID equals c.Action_Id
 group new {a,c} by a.Program into p
 select new
 {
    Program = p.Key,
    actionsQty = p.Count ( ),
    totalValue1 = p.Sum(y => y.c.Value1),
    totalValue2 = p.Sum (y => y.c.Value2),
    totalValue3 = p.Sum(y=>y.c.Value3)
};

Does something like this work?

var ListByPrograma  = from a in db.Actions
 join c in db.Costs on a.ID equals c.Action_Id
 group new {a,c} by a.Program into p
 select new
 {
    Program = p.Key,
    actionsQty = p.Count ( ),
    totalValue1 = p.Sum(y => y.c.Value1),
    totalValue2 = p.Sum (y => y.c.Value2),
    totalValue3 = p.Sum(y=>y.c.Value3)
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文