I'm trying to plot a flot chart. I want the x axis to be months from january to december and y axis to be account balances. I have the income and expense of the account over 12 months time but subtracting them would only give me the difference for the month it does not add the balance from the previous month.
Here is how I obtained the income and expenses over the range:
var monthsToDate = Enumerable.Range(1, 12)
.Select(m => new DateTime(DateTime.Today.Year, m, 1))
.ToList();
var sums = from month in monthsToDate
select new
{
month = month,
income = (from account in household.Accounts
from transaction in account.Transactions
where transaction.IsIncome && transaction.Created.Month == month.Month
select transaction.Amount).DefaultIfEmpty().Sum(),
expense = (from account in household.Accounts
from transaction in account.Transactions
where !transaction.IsIncome && transaction.Created.Month == month.Month
select transaction.Amount).DefaultIfEmpty().Sum(),
};
what I get back is this
.
.
.
[4] = { month = {5/1/2015 12:00:00 AM}, income = 3000, expense = 1804.75 }
[5] = { month = {6/1/2015 12:00:00 AM}, income = 2500, expense = 1560 }
[6] = { month = {7/1/2015 12:00:00 AM}, income = 0, expense = 550 }
.
.
.
You can add this reusable extension method to your code:
Example usage:
Now, if you change your
select
to return a named type, instead of an anonymous one (I'm assuming you're usingdecimal
for money - if not, you can adapt this code):Then you only have to add one easy line: