【问题标题】:Calculate Running Total in Linq在 Linq 中计算运行总计
【发布时间】:2018-05-11 18:13:56
【问题描述】:

我正在尝试计算我的两列的余额。我尝试了很多方法。但没有运气。

例如:

我想得到这样的结果:


借记    贷记     余额
=====     ======     =======
125.00     0.00     125.00
236.00     0.00    361.00
0.00     100.00     261.00

我的代码

var filteredSales = (from av in db.AccountVouchers
            join l in db.Ledgers on new {LedgerID = (Int32) av.LedgerID} equals new     {LedgerID = l.LedgerID}
            join sm in db.SalesMasters on av.VoucherNo equals sm.BillNo.ToString()
            where (av.VoucherDate >= FromDate && av.VoucherDate <= ToDate && av.LedgerID == LedgerID)
            group new {av, l, sm} by new
            {
                av.VoucherDate,
                l.LedgerName,
                av.VoucherType,
                av.VoucherNo,
                sm.REFNO,
                av.Debit,
                av.Credit,
                av.Narration
            }
            into g
            select new
            {

                g.Key.VoucherDate,
                g.Key.LedgerName,
                g.Key.VoucherType,
                VoucherNo = (g.Key.VoucherType != "SALES" ? g.Key.REFNO : g.Key.VoucherNo),
                //g.Key.VoucherNo,
                g.Key.Debit,
                g.Key.Credit,
                g.Key.Narration,
                dSum = g.Sum(s => s.av.Debit),

                //Balance=g.Key.Debit-g.Key.Credit,

              //  TBal = int.Parse(TBal) + (g.Key.Debit != 0 ? TBal + g.Key.Debit : TBal - g.Key.Credit))


                             }).ToList();

【问题讨论】:

  • Caculate balance 是什么意思?你只需要显示你想要的结果
  • 你想做什么?运行总数?还是您也需要更新记录?

标签: c# asp.net-mvc linq


【解决方案1】:

终于明白你的问题了:

void Main()
{
    var list=new List<test>
    {
      new test{ Debit=125, Credit=0},
      new test{ Debit=236,Credit=0},
      new test{ Debit=0, Credit=100},
    };
    if(list.Any())
    {
       var first = list.First();
       first.Balance = first.Debit - first.Credit;
       list.Aggregate((x,y)=>{ y.Balance = x.Balance + y.Debit - y.Credit; return y;});
    }

    list.Dump();
}
class test
{
   public int Debit {get;set;}
   public int Credit {get;set;}
   public int Balance {get;set;}
}

【讨论】:

    【解决方案2】:

    这显示了 Tim 示例的替代语法。

    void Main()
    {
        var list=new List<test>
        {
          new test{ Debit=125, Credit=0},
          new test{ Debit=236,Credit=0},
          new test{ Debit=0, Credit=100},
        };
        int balance = 0;
        list = list.Select( i=> { 
            balance += i.Debit - i.Credit;
            i.Balance = balance;
            return i;
            }).ToList();
        list.Dump();
    }
    class test
    {
       public int Debit {get;set;}
       public int Credit {get;set;}
       public int Balance {get;set;}
    }
    

    这取自答案here,它正在运行总计。正如它在 cmets 中提到的那样,您应该小心不要导致执行多次发生,因为余额值将关闭。使用 ToList() 应该可以解决这个问题。

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    • 感谢 jww 的反馈,我更新了答案以包含代码示例。
    【解决方案3】:

    更简单的解决方案:

    var result=accountVouchers.Select((x, i) => new AccountVoucher { Debit = x.Debit, Credit = x.Credit, Balance = list.Take(i+1).Sum(y => y.Debit - y.Credit) });
    

    完整代码:

    using System.Collections.Generic;
    using System.Linq;
    
    namespace ConsoleApp1
    {
        class Program
        {
            static void Main(string[] args)
            {
                var accountVouchers= new List<AccountVoucher>
                {
                    new AccountVoucher{ Debit=125, Credit=0},
                    new AccountVoucher{ Debit=236,Credit=0},
                    new AccountVoucher{ Debit=0, Credit=100},
                };
                var result=accountVouchers.Select((x, i) => new AccountVoucher { Debit = x.Debit, Credit = x.Credit, Balance = list.Take(i+1).Sum(y => y.Debit - y.Credit) });
            }
        }
        class AccountVoucher
        {
            public int Debit { get; set; }
            public int Credit { get; set; }
            public int Balance { get; set; }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-29
      • 2010-10-14
      • 1970-01-01
      • 2017-10-04
      • 2010-10-26
      • 2023-03-23
      相关资源
      最近更新 更多