I have a database table which looks like the following:
| ID | Label | Type | Amount | Category | OriginDate |
------------------------------------------------------
| 1 | Foo | 1 | 100 | 8 | 2017-01-23 |
| 2 | Bar | 2 | 250 | 1 | 2017-01-30 |`
| 3 | Foo | 1 | 400 | 12 | 2017-02-15 |`
Basically what I am trying to achieve is this. I am trying to write a linq query where I can group each record within a month. Then sum up all the values of the column Amount, and store it along it's corresponding month group.
Sums will look like the following, given the example table above:
- January = 350
- February = 400
These associated sums will then be displayed in a bar chart.
I imagine that I will need to do some grouping, and further expand using some foreach-magic. But I am quite blank on how to achieve it at this point.
Any suggestion that could point me in the right direction will be much appreciated.
EDIT:
jdweng's linq query put me on the right track.
Though it's far from optimal, the below code is how I solved it. Any pointers in terms of improvement are most welcome.
public ExpensesByMonth ChartMonthlyExpenses()
{
// Retreives all transactions from repository
List<Transaction> allTransactions = _repository.SelectAllTransactions();
// Filter transactions by transaction type (2 = expenses)
var filteredTransactions = (from transactions in allTransactions
where transactions.Type == 2
select transactions);
// Made some modifications to jdweng's query in order to adapt it to existing codebase
var results = filteredTransactions.GroupBy(x => new { month = x.OriginDate.Month, year = x.OriginDate.Year }).Select(x => new {
month = x.Key.month,
year = x.Key.year,
amount = x.Select(y => y.Amount).Sum()
}).ToList();
// Instantiating a new object containing all month's expenses as properties
ExpensesByMonth expenseObj = new ExpensesByMonth();
// Looping through each result and assigning the amounts to a certain property, using a switch statement
foreach(var result in results)
{
switch (result.month)
{
case 1:
expenseObj.JanuaryExpenses = result.amount;
break;
case 2:
expenseObj.FebruaryExpenses = result.amount;
break;
case 3:
expenseObj.MarchExpenses = result.amount;
break;
case 4:
expenseObj.AprilExpenses = result.amount;
break;
case 5:
expenseObj.MayExpenses = result.amount;
break;
case 6:
expenseObj.JuneExpenses = result.amount;
break;
case 7:
expenseObj.JulyExpenses = result.amount;
break;
case 8:
expenseObj.AugustExpenses = result.amount;
break;
case 9:
expenseObj.SeptemberExpenses = result.amount;
break;
case 10:
expenseObj.OctoberExpenses = result.amount;
break;
case 11:
expenseObj.NovemberExpenses = result.amount;
break;
case 12:
expenseObj.DecemberExpenses = result.amount;
break;
}
}
return expenseObj;
}
`