计算Linq中的运行总数

本文关键字:运行 Linq 计算 | 更新日期: 2023-09-27 18:20:32

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

例如:

我想得到这样的结果:


借记 nbsp信贷 nbsp余额
===== nbsp;==== nbsp ======
125.00 nbsp;0.00 nbsp;125
236.00 nbsp;0.00 nbsp;361
0.00 nbsp;100.00 nbsp;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();

计算Linq中的运行总数

最后,我理解了你的问题:

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;}
}

这显示了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;}
}

这是从这里的答案中得出的,这是一个连续的总数。正如它在评论中提到的那样,你应该小心,不要因为平衡值将被关闭而导致多次执行。使用ToList()应该注意这一点。

更简单的解决方案:

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; }
    }
}