public ActionResult ProfitAndLoss(string date1, string date2) { try { ProfitAndLossRepository pRepo = new ProfitAndLossRepository(); var entries = pRepo.GetEntries(); ViewBag.TableTitle = "as at " + new ConfigurationRepository().GetFirst().FinancialDate.ToString("D"); if (!(String.IsNullOrEmpty(date1) || (String.IsNullOrEmpty(date2)))) { entries = pRepo.GetEntries(Convert.ToDateTime(date1), Convert.ToDateTime(date2)); ViewBag.TableTitle = "Between " + Convert.ToDateTime(date1).ToString("D") + " and " + Convert.ToDateTime(date2).ToString("D"); } //entries = entries.OrderBy(e => e.EntryType).ToList(); var sortedEntries = new List <ExpenseIncomeEntry>(); foreach (var entry in entries) { var item = sortedEntries.FirstOrDefault(s => s.AccountName.ToUpper().Equals(entry.AccountName.ToUpper())); if (item == null) { item = entry; sortedEntries.Add(item); } else //for item(s) that occur(s) twice, amt spent or earned = difference of the balances for the two occurences (days) { item.Amount -= entry.Amount; //getting the difference in the account balances within the specified dates } } ViewBag.SumOfIncome = sortedEntries.Where(en => en.EntryType == PandLType.Income).Sum(e => e.Amount); ViewBag.SumOfExpense = sortedEntries.Where(en => en.EntryType == PandLType.Expenses).Sum(e => e.Amount); ViewBag.Profit = (decimal)ViewBag.SumOfIncome - (decimal)ViewBag.SumOfExpense; return(View(sortedEntries)); } catch (Exception ex) { ErrorLogger.Log("Message= " + ex.Message + "\nInner Exception= " + ex.InnerException + "\n"); return(PartialView("Error")); } }