Linq 根据两列对组求和

发布于 2024-09-04 12:03:35 字数 631 浏览 5 评论 0原文

我有一个类如下:

Class Financial
{
    string Debit;
    string Credit;
    decimal Amount;
}

我有一个包含此类对象的列表,其中包含多个记录。我所需要的只是执行分组总和,就像在 sql 中

Select debit, Credit, sum(amount) group by Debit, Credit

我尝试使用如下语句一样:

from operation in m_listOperations
    orderby operation.Debit, operation.Credit ascending
    group operation by operation.Debit, operation.Credit into groupedOperation
    select new Financial(Debit, Credit, 
                groupedOperation.Sum(operation => operation.Amount))

但这不起作用,因为我无法对两列进行分组。

有什么建议吗?

I have a class as below:

Class Financial
{
    string Debit;
    string Credit;
    decimal Amount;
}

And I have a list with objects of this class, with multiple records. All I need is to perform a groupped sum, something like in sql

Select debit, Credit, sum(amount) group by Debit, Credit

I tried with a statement as below:

from operation in m_listOperations
    orderby operation.Debit, operation.Credit ascending
    group operation by operation.Debit, operation.Credit into groupedOperation
    select new Financial(Debit, Credit, 
                groupedOperation.Sum(operation => operation.Amount))

But this doesn't work since I cannot group on two columns.

Any suggestions?

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

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

发布评论

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

评论(1

北恋 2024-09-11 12:03:35
...
group operation by new { operation.Debit, operation.Credit } into groupedOperation
...
...
group operation by new { operation.Debit, operation.Credit } into groupedOperation
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文