2012-07-17 36 views
2

我有以下SQL查詢和我試圖將其轉換爲LINQ,但沒有成功:分組和點心LINQ到實體

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

Result of Query

希望有人能指導我在此。

謝謝。

回答

3

下應該是可比的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) 
       }; 
0
var query = 
    from at in account_transactions 
    where at.financialaccountid == 47 
    && at.ledgervisible == 1 
    group at.financialaccountid into g 
    select new 
    { 
     financialaccountid = g.Key, 
     balance = g.Sum(at => at.creditamount) - g.Sum(at => at.debitamount) 
    };