如何编写结合 group by 和聚合的 LINQ 查询?

发布于 2024-11-05 08:52:40 字数 504 浏览 0 评论 0原文

给定以下输入,如何编写 LINQ 查询或表达式来返回数量的聚合结果集?

输入:

var foo = new[] { new { PO = "1", Line = 2, QTY = 0.5000 }, 
                  new { PO = "1", Line = 2, QTY = 0.2500 }, 
                  new { PO = "1", Line = 2, QTY = 0.1000 }, 
                  new { PO = "1", Line = 2, QTY = -0.1000 } 
                }.ToList();

期望的结果:

类似于

new { PO = "1", Line = 2, QTY = 0.7500 } // .5 + .25 + .1 + -.1

我如何为多行编写它(请参阅 foo 中的对象模型)?

Given the following input, how do I write a LINQ query or expression to return an aggregated result set for the quantity?

Input:

var foo = new[] { new { PO = "1", Line = 2, QTY = 0.5000 }, 
                  new { PO = "1", Line = 2, QTY = 0.2500 }, 
                  new { PO = "1", Line = 2, QTY = 0.1000 }, 
                  new { PO = "1", Line = 2, QTY = -0.1000 } 
                }.ToList();

Desired result:

Something along the lines of

new { PO = "1", Line = 2, QTY = 0.7500 } // .5 + .25 + .1 + -.1

How would I write it for multiple lines as well (see the object model in foo)?

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

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

发布评论

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

评论(2

万人眼中万个我 2024-11-12 08:52:40

怎么样:

var result = foo.GroupBy(x => x.Line)
                .Select(g => new { PO = g.First().PO, 
                                   Line = g.Key, 
                                   QTY = g.Sum(x => x.QTY) });

如果你只有一行,只需添加一个 .Single() - result 是一个匿名类型的 IEnumerable在设置 foo 时定义。

编辑:

如果 PO 和线路都应指定不同的组(PO 可以具有不同的值),则它们都必须是组键的一部分:

var result = foo.GroupBy(x => new { x.PO, x.Line})
                .Select(g => new { 
                    PO = g.Key.PO, 
                    Line = g.Key.Line, 
                    QTY = g.Sum(x => x.QTY) 
                  });

How about this:

var result = foo.GroupBy(x => x.Line)
                .Select(g => new { PO = g.First().PO, 
                                   Line = g.Key, 
                                   QTY = g.Sum(x => x.QTY) });

In the case you just have one Line, just add a .Single() - result is an IEnumerable of the anonymous type defined when you set up foo.

Edit:

If both PO and Line should designate different groups (PO can have different values), they both have to be part of the group key:

var result = foo.GroupBy(x => new { x.PO, x.Line})
                .Select(g => new { 
                    PO = g.Key.PO, 
                    Line = g.Key.Line, 
                    QTY = g.Sum(x => x.QTY) 
                  });
江心雾 2024-11-12 08:52:40
var query = (from t in foo
                         group t by new {t.PO, t.Line}
                         into grp
                             select new
                                        {
                                            grp.Key.PO,
                                            grp.Key.Line,
                                            QTY = grp.Sum(t => t.QTY)
                                        }).ToList()
var query = (from t in foo
                         group t by new {t.PO, t.Line}
                         into grp
                             select new
                                        {
                                            grp.Key.PO,
                                            grp.Key.Line,
                                            QTY = grp.Sum(t => t.QTY)
                                        }).ToList()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文