private static BigTable BuildBigTable(Database database, IEnumerable <Account> accounts, DateTime fromDate, DateTime toDate) { var transactionGroups = database.Transactions .Where(t => accounts.Contains(t.Account) && t.ValueDate >= fromDate && t.ValueDate <= toDate) .GroupBy(c => c.Category); var bigTable = new BigTable { Dates = new DateTime[(toDate.Year - fromDate.Year) * 12 + (toDate.Month - fromDate.Month) + 1], }; for (var i = 0; i < bigTable.Dates.Length; i++) { bigTable.Dates[i] = fromDate.AddMonths(i); } foreach (var group in transactionGroups) { string?categoryName = null; string?categoryGroupName = null; if (group.Key != null) { var category = group.Key; categoryName = category.Name; categoryGroupName = category.GroupName; } var bigTableGroup = bigTable.CategoryGroups.Find(g => string.Equals(g.Name, categoryGroupName, StringComparison.Ordinal)); if (bigTableGroup == null) { bigTableGroup = new BigTableCategoryGroup(bigTable) { Name = categoryGroupName, }; bigTable.CategoryGroups.Add(bigTableGroup); } var bigTableCategory = new BigTableCategory(bigTableGroup) { Name = categoryName, }; bigTableGroup.Categories.Add(bigTableCategory); foreach (var transaction in group) { var index = (transaction.ValueDate.Year - fromDate.Year) * 12 + (transaction.ValueDate.Month - fromDate.Month); bigTableCategory.Totals[index].Add(transaction.Amount); } } bigTable.ComputeTotals(); bigTable.CategoryGroups.Sort(NameComparer.Instance); foreach (var categoryGroup in bigTable.CategoryGroups) { categoryGroup.Categories.Sort(NameComparer.Instance); } return(bigTable); }
public int Compare(BigTableCategory x, BigTableCategory y) { return(Compare(x.Name, y.Name)); }