Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以Linq计算运行总数_C#_Asp.net Mvc_Linq - Fatal编程技术网

C# 以Linq计算运行总数

C# 以Linq计算运行总数,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,我正在计算我的两列的余额。我尝试了很多方法,但是没有成功 例如: 我想得到这样的结果: 借方贷方余额 ========================== 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)

我正在计算我的两列的余额。我尝试了很多方法,但是没有成功

例如:

我想得到这样的结果:


借方贷方余额
==========================
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();
var filteredSales=(来自db.account中的av)
在新的{LedgerID=(Int32)av.LedgerID}等于新的{LedgerID=l.LedgerID}上的db.Ledgers中加入l
将sm加入av.VoucherNo上的db.SalesMasters等于sm.BillNo.ToString()
其中(av.VoucherDate>=起始日期和av.VoucherDate s.av.借方),
//余额=g.Key.Debit-g.Key.Credit,
//TBal=int.Parse(TBal)+(g.Key.Debit!=0?TBal+g.Key.Debit:TBal-g.Key.Credit))
}).ToList();

这显示了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;}
}
void Main()
{
变量列表=新列表
{
新测试{Debit=125,Credit=0},
新测试{Debit=236,Credit=0},
新测试{借方=0,贷方=100},
};
国际收支平衡=0;
列表=列表。选择(i=>{
余额+=一借一贷;
i、 平衡=平衡;
返回i;
}).ToList();
list.Dump();
}
课堂测试
{
公共整数借方{get;set;}
公共积分{get;set;}
公共整数平衡{get;set;}
}

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

最后,我理解您的问题:

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;}
}
void Main()
{
变量列表=新列表
{
新测试{Debit=125,Credit=0},
新测试{Debit=236,Credit=0},
新测试{借方=0,贷方=100},
};
if(list.Any())
{
var first=list.first();
第一。余额=第一。借方-第一。贷方;
合计((x,y)=>{y.余额=x.余额+y.借方-y.贷方;返回y;});
}
list.Dump();
}
课堂测试
{
公共整数借方{get;set;}
公共积分{get;set;}
公共整数平衡{get;set;}
}
更简单的解决方案:

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; }
    }
}
使用System.Collections.Generic;
使用System.Linq;
名称空间控制台EAPP1
{
班级计划
{
静态void Main(字符串[]参数)
{
var Account凭单=新列表
{
新会计凭证{借方=125,贷方=0},
新会计凭证{借方=236,贷方=0},
新会计凭证{借方=0,贷方=100},
};
var结果=会计凭证。选择((x,i)=>新会计凭证{Debit=x.Debit,Credit=x.Credit,Balance=list.Take(i+1).Sum(y=>y.Debit-y.Credit)});
}
}
班级会计凭证
{
公共整数借方{get;set;}
公共积分{get;set;}
公共整数平衡{get;set;}
}
}

计算余额是什么意思?你只是展示你想要的结果你想做什么?总数是多少?或者您也需要更新记录吗?虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接以供参考。如果链接页面发生更改,只有链接的答案可能会无效。感谢jww的反馈,我更新了我的答案以包含代码示例。