C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,029 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have some data on the left side of the image below. I want to create a balance seed like the one on the right side using
Screenshot (22).png
sorry, minor bug (I edited the post and updated)
n.balance = n.balance + p.Sum(r => r.balance);
with
n.balance = n.balance + p.LastOrDefault()?.balance ?? 0;
only need the previous balance, not recalc running total
you need a class for the result data:
class DataDisplay
{
public string text {get; set;}
public Decimal credited {get; set;}
public Decimal debited {get; set;}
public Decimal balance {get; set;}
}
then it pretty simple.
var list = getInputData();
var previousBalanceDate = new DateTime(2024, 3,16);
var resultList = list
.OrderBy(r => r.date)
.Select(r => new DataDisplay
{
text = r.date < previousBalanceDate ? "previous balance" : r.date.ToString("dd-MMM-yy"),
credited = r.credited,
debited = r.debited,
balance = r.credited - r.debited
})
.Aggregate(new List<DataDisplay>() as IEnumerable<DataDisplay>, (p, n) =>
{
n.balance = n.balance + p.LastOrDefault()?.balance ?? 0;
if (n.text == "previous balance" && p.FirstOrDefault()?.text == "previous balance")
{
p.First().balance = n.balance;
return p;
}
return p.Append(n);
});