在linq加入后得到总和

时间:2015-02-20 21:00:24

标签: c# linq

我正在使用此linq查询来获取双打列表。该列表看起来正确,但现在我需要在列表中总计双打。

        var total = from sauId in schools
                                    join item in entities.AmountTable
                                    on sauId equals item.sauId
                                    select item.amount;

3 个答案:

答案 0 :(得分:3)

对结果使用Sum()

   var total = (from sauId in schools
               join item in entities.AmountTable
               on sauId equals item.sauId
               select item.amount).Sum();

如果item.amountdouble,则total也是double

答案 1 :(得分:0)

对于任何寻求流利语法的人,我相信这将是这样做的方式:

var total = schools
   .Join(
      entities.AmountTable,
      s => s.sauId,
      at => at.sauId,
      (s, at) => at.amount)
   .Sum();

答案 2 :(得分:0)

尝试按sauId

对数据进行分组
var qry = from item in entities.AmountTable
         group item by item.sauId into grp
         select new {
             sauId = grp.Key,
             amuont = grp.Sum(c=>c.amount)
         };