public IEnumerable <KeyValuePair <int, decimal> > GetSumByYear(IncomeGetSumBinding binding) { using (var context = GetMainContext()) { int startYear = binding.From?.Year ?? context.Incomes.WhereUser(UserId).OrderBy(x => x.Date).FirstOrDefault().Date.Year; int endYear = binding.To?.Year ?? DateTime.Now.Year; var years = Enumerable.Range(startYear, endYear - startYear + 1); var periods = years.Select(x => new FilteredBinding(new DateTime(x, 1, 1), new DateTime(x, 12, 31))); var tasks = periods.Select(x => new KeyValuePair <int, Task <decimal> >(x.From.Value.Year, GetSum(binding.OverrideFromTo <IncomeGetSumBinding>(x.From, x.To)))); return(tasks.Select(x => new KeyValuePair <int, decimal>(x.Key, x.Value.Result))); } }
public IEnumerable <KeyValuePair <string, decimal> > GetSumByMonthOfYear(IncomeGetSumBinding binding) { using (var context = GetMainContext()) { var from = binding.From ?? context.Incomes.WhereUser(UserId).OrderBy(x => x.Date).FirstOrDefault().Date; var to = binding.To ?? DateTime.Now; var periods = from.RangeMonthsClosed(to) .Select(x => new FilteredBinding(x.from, x.to)) .ToList(); var tasks = periods.Select(x => new KeyValuePair <FilteredBinding, Task <decimal> >(x, GetSum(binding.OverrideFromTo <IncomeGetSumBinding>(x.From, x.To)))); return(tasks.Select(x => new KeyValuePair <string, decimal>($"{x.Key.From.Value.Year}-{x.Key.From.Value.Month}", x.Value.Result))); } }