Linq基于两列对组进行求和

时间:2010-06-07 12:00:25

标签: linq group-by sum linq-group

我有一个课程如下:

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))

但是这不起作用,因为我不能分成两列。

有什么建议吗?

1 个答案:

答案 0 :(得分:2)

...
group operation by new { operation.Debit, operation.Credit } into groupedOperation
...