分组 lambda

发布于 2024-09-26 12:35:24 字数 410 浏览 1 评论 0原文

如果我有这样的结构:

Batch  Amount
76  495.4
76  975.75
76  25
76  442.46
77  1335.12
77  2272.37
77  34.5
77  496.99
77  360
77  13
77  594.6

我想得到类似

Batch  Amount
76    1938.61
77    5106.58

表达式应该如何?

我是这样开始的:

batches.GroupBy(x => new { Batch = x.Batch, Amount = x.Amount });

但这并不是我正在寻找的东西。帮助我把它做好。谢谢

if I have a structure like this:

Batch  Amount
76  495.4
76  975.75
76  25
76  442.46
77  1335.12
77  2272.37
77  34.5
77  496.99
77  360
77  13
77  594.6

And I want to get something like

Batch  Amount
76    1938.61
77    5106.58

How the expression should be?

I started with something like:

batches.GroupBy(x => new { Batch = x.Batch, Amount = x.Amount });

But it's not quite the thing that I'm looking for. Help me to get it right. Thanks

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

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

发布评论

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

评论(2

调妓 2024-10-03 12:35:24

关闭。试试这个:

batches.GroupBy(x => x.Batch, x => x.Amount).Select(g => new {
    Batch = g.Key,
    Amount = g.Sum()
});

Close. Try this:

batches.GroupBy(x => x.Batch, x => x.Amount).Select(g => new {
    Batch = g.Key,
    Amount = g.Sum()
});
如歌彻婉言 2024-10-03 12:35:24

查询将如下所示:

var bs = from s in batches
            group s by s.Batch into g
            select new { Batch = g.Key, Amount = g.Sum(p => p.Amount) };

虽然我不知道更新的 lambda 等价物是什么

Resharper 帮助很大! :)

var batches = bs .GroupBy(s => s.Batch).Select(
                g => new {Batch = g.Key, Amount = g.Sum(p => p.Amount)});

The query will be look like:

var bs = from s in batches
            group s by s.Batch into g
            select new { Batch = g.Key, Amount = g.Sum(p => p.Amount) };

Although I don't know what's the lambda equivalent would be

upd:

Resharper helps a lot! :)

var batches = bs .GroupBy(s => s.Batch).Select(
                g => new {Batch = g.Key, Amount = g.Sum(p => p.Amount)});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文