【问题标题】:C# - LINQ - Sum a field from other tableC# - LINQ - 对其他表中的字段求和
【发布时间】:2017-03-31 14:22:40
【问题描述】:

我需要加入两个表(Movimientos 和 Cuentas),按 CuentasId 分组并生成 Movimientos.Monto 的总和
Movimientos 有一个 CuentasId 加入这个,我可以从 Cuentas 获取数据但无法获取 Sum。

这是我最好的方法,任何帮助都会受到赞赏,我对语法有点困惑。在此先感谢和亲切的问候,

          var cuentas = (from mov in _data.Movimientos
                               join ct in _data.Cuentas
                               on mov.CuentasId equals ct.CuentasId
                               where ct.IsDeleted == 0 && mov.IsDeleted == 0
                               group ct by new
                               {
                                   CuentasId = ct.CuentasId,
                                   Alias = ct.Alias,
                                   Moneda = ct.Monedas.Nombre,
                                   Signo = ct.Monedas.Signo,
                                  Banco = ct.Bancos.Nombre
                               } into ctg
                               select new
                               {
                                   Alias = ctg.Key.Alias,
                                   Moneda = ctg.Key.Moneda,
                                   Signo = ctg.Key.Signo,
                                   Banco = ctg.Key.Banco,
                                   Monto = ctg.Sum(mov.Monto)
                               }
                                ).ToList();

【问题讨论】:

    标签: c# .net linq group-by sum


    【解决方案1】:

    您需要像这样对要求和的值进行分组

    group mov.Monto by new { ..... } into ctg
    

    然后ctg 将是mov.Monto 值的集合,这些值按您的ct 属性列表分组,您只需在ctg 上调用Sum select

    Monto = ctg.Sum()
    

    所以你的新查询是

    var cuentas = (from mov in _data.Movimientos
                   join ct in _data.Cuentas
                   on mov.CuentasId equals ct.CuentasId
                   where ct.IsDeleted == 0 && mov.IsDeleted == 0
                   group mov.Monto by new
                   {
                       CuentasId = ct.CuentasId,
                       Alias = ct.Alias,
                       Moneda = ct.Monedas.Nombre,
                       Signo = ct.Monedas.Signo,
                       Banco = ct.Bancos.Nombre
                   } into ctg
                   select new
                   {
                       Alias = ctg.Key.Alias,
                       Moneda = ctg.Key.Moneda,
                       Signo = ctg.Key.Signo,
                       Banco = ctg.Key.Banco,
                       Monto = ctg.Sum()
                   }).ToList();
    

    【讨论】:

    • 谢谢@juharr。这必须添加还是只替换实际的组?
    • @LeandroTupone 基本上你用group mov.Monto 代替group ct 然后ctg 将是一组Monto 值的集合,你可以调用Sum。其他一切都将保持不变。
    • 谢谢,现在我明白了 LINQ 中 group by 背后的逻辑
    【解决方案2】:

    您也可以尝试先分组,然后再将项目相加:

    var cuentas = (from mov in _data.Movimientos.Where(w => w.IsDeleted == 0).GroupBy(g => g.CuentasId)
                  join ct in _data.Cuentas.Where(w => w.IsDeleted == 0).GroupBy(g => new { CuentasId = g.CuentasId, Alias = g.Alias, Monedas = g.Monedas.Nombre, Signo = g.Monedas.Signo, Banco = g.Bancos.Nombre })
                  on mov.Key.CuentasId equals ct.Key.CuentasId
                  select new
                  {
                      Alias = ct.Key.Alias,
                      Moneda = ct.Key.Moneda,
                      Signo = ct.Key.Signo,
                      Banco = ct.Key.Banco,
                      Monto = mov.Sum(s => s.Monto)
                   }
                   ).ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-16
      • 2015-04-15
      • 1970-01-01
      • 2011-12-09
      • 1970-01-01
      • 1970-01-01
      • 2019-09-15
      • 1970-01-01
      相关资源
      最近更新 更多