public double Query(DateTime start, DateTime end) { if (end < start) { return(0); } var startYearMonth = Convert.ToInt32(start.ToString("yyyyMM")); var endYearMonth = Convert.ToInt32(end.ToString("yyyyMM")); var searchBudgetResult = _repo.getAll().Where(x => startYearMonth <= Convert.ToInt32(x.YearMonth) && Convert.ToInt32(x.YearMonth) <= endYearMonth).ToList(); if (!searchBudgetResult.Any()) { return(0); } else { var allAmount = 0; foreach (var monthBudget in searchBudgetResult) { var monthStart = monthBudget.YearMonth == start.ToString("yyyyMM") ? start : getMonthFirstDay(monthBudget); var monthEnd = monthBudget.YearMonth == end.ToString("yyyyMM") ? end : getMonthEndDay(monthBudget); allAmount += getMonthAmount(monthBudget, monthStart, monthEnd); } return(allAmount); } }
public double Query(DateTime start, DateTime end) { if (end < start) { return(0); } var startYearMonth = Convert.ToInt32(start.ToString("yyyyMM")); var endYearMonth = Convert.ToInt32(end.ToString("yyyyMM")); var searchBudgetResult = _repo.getAll().Where(x => startYearMonth <= Convert.ToInt32(x.YearMonth) && Convert.ToInt32(x.YearMonth) <= endYearMonth).ToList(); if (!searchBudgetResult.Any()) { return(0); } else { var allAmount = 0; foreach (var monthBudget in searchBudgetResult) { DateTime monthStart = start; DateTime monthEnd = end; var year = Convert.ToInt32(monthBudget.YearMonth.Substring(0, 4)); var month = Convert.ToInt32(monthBudget.YearMonth.Substring(4, 2)); if (monthBudget.YearMonth == startYearMonth.ToString()) { monthStart = start; monthEnd = new DateTime(year, month, DateTime.DaysInMonth(start.Year, start.Month)); } if (monthBudget.YearMonth == endYearMonth.ToString()) { monthStart = new DateTime(year, month, 1); monthEnd = end; } if (monthBudget.YearMonth != startYearMonth.ToString() && monthBudget.YearMonth != endYearMonth.ToString()) { monthStart = new DateTime(year, month, 1); monthEnd = new DateTime(year, month, DateTime.DaysInMonth(year, month)); } var daydiff = (monthEnd - monthStart).Days + 1; allAmount += monthBudget.Amount / DateTime.DaysInMonth(monthStart.Year, monthStart.Month) * daydiff; } return(allAmount); } }