对实体进行分组和求和

VAA*_*AAA 4 sql linq linq-to-entities

我有以下 SQL 查询,我尝试将其转换为 LINQ,但没有成功:

select 
at.financialaccountid,
SUM(creditamount)-SUM(debitamount)
from account_transactions at
where at.financialaccountid = 47
and ledgervisible = 1
GROUP BY at.financialaccountid
Run Code Online (Sandbox Code Playgroud)

查询结果

希望有人可以指导我。

谢谢。

eul*_*rfx 5

以下应该是一个类似的 C# LINQ 实现:

class AccountTransaction
{
    public int FinancialAccountId { get; set; }
    public bool LedgerVisible { get; set; }
    public decimal CreditAmount { get; set; }
    public decimal DebitAmount { get; set; }
}

var transactions = new List<AccountTransaction>();

var results = from at in transactions
              where at.FinancialAccountId == 4 && at.LedgerVisible == true
              group at by at.FinancialAccountId into g
              select new
              {
                  FinancialAccountId = g.Key,
                  Delta = g.Sum(x => x.CreditAmount) - g.Sum(x => x.DebitAmount)
              };
Run Code Online (Sandbox Code Playgroud)