Linq - 内联合与总和

时间:2015-03-27 14:42:18

标签: linq

我有2个表,第一个表是交易

交易表:

 Id        LoanId
 1          100

我的第二个表是TransactionLeg表

transactionleg table:

ID  TransactionId GLAmount
1       1            200
2       1            200

我想在transactionId列中加入两个,按照loanID分组并将GLAmount加起来

所以它应该产生以下内容:

LoanId TotalGlAmount   100 400

var investmentsWritten = from transaction in ctx.Transactions
                         join transactionleg in ctx.TransLegs on transaction.Id equals transactionleg.TransactionId
                         where
                         transaction.Class == Transaction.TransactionClasses.WriteOff &&
                         transaction.Created >= StartDate.Date && transaction.Created <= EndDate.Date
                         group transaction by transaction.LoanId
                         into g
                         select new { Id = g.Key, Value = _____ };

我想知道下划线的位置是什么,值是transactionleg.GLAmount。我尝试了g.Sum(x =&gt; x.GLAmount)但GLAmount无法识别,它以红色突出显示Cannot resolve symbol GLAmount

1 个答案:

答案 0 :(得分:1)

这是因为GLAmount位于transactionleg而不是transaction, 像这样修改你的小组:

group new 
     { 
       transaction,
       transactionleg 
     } 
  by new
     {
       transaction.LoanId
     }

现在选择:

select new 
      { 
        Id = g.Key, 
        Value = g.Sum(x=>x.transactionleg.GLAmount) 
      }