Skip to content
Advertisement

How to compute running balance from array in ASP.NET MVC controller?

This is my code in controller

var localrecord = db.spGetTotalRecordsBy3m(accountNUmber)
                    .OrderBy(x => x.Date).ToArray();

I want to implement running balance in this array. This query will return Date, debit, credit and balance columns. I have to compute balance on the basis of debit and credit.

I want to achieve this:

balance = balance - debit - credit

for each row of the array.

Hope, you get my point…help me….thanks

Advertisement

Answer

I have solved the problem. In this I store array in localrecord and previous balance in Prev. then I loop throughthe array data.

var localrecord = db.spGetTotalRecordsBy3m(branch, basic, suffix).ToArray();

                var prev = db.spGetTotalRecordsBy3m(branch, basic, suffix).Select(x => x.Balance).FirstOrDefault();

                for (int i = 0; i < localrecord.Length; i++)
                {
                    localrecord[i].Balance = prev - localrecord[i].Debit - localrecord[i].Credit;
                    prev = localrecord[i].Balance;
                }

Hope, it will help someone..

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement