public ActionResult CancelLoanRepayments(sdtoViewLoanRepayments Repayments)
        {
            List<sdtoLoanRepayment> RepaymentCancelList = new List<sdtoLoanRepayment>();
            string[] selectionRepayments = Repayments.InputSelection.Trim(" []".ToCharArray()).Split(',');
            if (selectionRepayments != null && selectionRepayments.Length > 0)
            {
                for (int iLoanRepaymentId = 0; iLoanRepaymentId < selectionRepayments.Length; iLoanRepaymentId++)
                {
                    if (selectionRepayments[iLoanRepaymentId].Equals("true", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var repayment = db.sdtoLoanRepayments.Find(iLoanRepaymentId);
                        RepaymentCancelList.Add(repayment);
                    }
                }

                bfTransaction objTransaction = new bfTransaction(db);
                foreach (var repayment in RepaymentCancelList)
                {
                    repayment.Status = RepaymentStatus.Cancelled;
                    db.Entry(repayment).State = EntityState.Modified;

                    objTransaction.CancelPostedLoanRepayment(repayment);
                }
                db.SaveChanges();

                SetDisplayMessage("Loan repayments are cancelled successfully");

                return RedirectToAction("Index", "LoanRepayments", new { LoanId = Repayments.LoanId });
            }
            return View(Repayments);
        }
        public ActionResult CancelLoanRepayments(long LoanId)
        {
            //var query = from c in db.sdtoLoanRepayments
            //join o in db.sdtoLoanInfoes on c.LoanId equals o.LoanId
            //where c.LoanId == ((LoanId == null || LoanId.Value == 0) ? c.LoanId : LoanId)
            //group c by c.LoanId into g
            //select new
            //{
            //  Name = g.Key,
            //  Sum = g.Sum(oi => oi.RepaymentAmount),
            //  RepaymentCode = c.RepaymentCode
            //};

            sdtoLoanRepayment repay = new sdtoLoanRepayment();
            var itemsLoan = db.sdtoLoanInfoes.Include(x => x.Member).Where(x => x.Status == LoanStatus.Active).ToList();
            var itemsLoans = itemsLoan.Select(x => new SelectListItem() { Value = x.LoanId.ToString(), Text = x.LoanId + " - " + x.LoanAmount + "[" + x.Member.FirstName + " " + x.Member.LastName + "]" }).ToList();
            itemsLoans.Insert(0, new SelectListItem() { Value = "0", Text = "Select a loan" });
            ViewBag.LoanList = new SelectList(itemsLoans, "Value", "Text");

            ViewBag.LoanDetails = db.sdtoLoanInfoes.Where(x => x.LoanId == LoanId).FirstOrDefault();

            //var sdtoLoanRepayments = db.sdtoLoanRepayments.Where(x => x.LoanId == ((LoanId == null || LoanId.Value == 0) ? x.LoanId : LoanId)).Include(s => s.LoanDetails);
            var sdtoLoanRepayments = db.sdtoLoanRepayments.Where(x => x.LoanId == LoanId && x.IsDeleted == false && x.Status != RepaymentStatus.Cancelled).Include(s => s.LoanDetails).Select(x => new sdtoViewRepaymentOfLoans()
            {
                ChequeDetails = x.ChequeDetails,
                InterestAmount = x.InterestAmount,
                InterestRate = x.InterestRate,
                LoanId = x.LoanId,
                LoanRepaymentId = x.LoanRepaymentId,
                Notes = x.Notes,
                PaymentMode = x.PaymentMode,
                PreviousPaymentDueAmount = x.PreviousPaymentDueAmount,
                PendingInstallments = x.PendingInstallments,
                PendingPrincipalAmount = x.PendingPrincipalAmount,
                PrincipalAmount = x.PrincipalAmount,
                RepaymentAmount = x.RepaymentAmount,
                RepaymentCode = x.RepaymentCode,
                RepaymentDate = x.RepaymentDate,
                Status = x.Status
            }).ToList();

            if (sdtoLoanRepayments != null && sdtoLoanRepayments.Count() > 0)
            {
                ViewBag.TotalPaid = sdtoLoanRepayments.Sum(y => y.RepaymentAmount);
                ViewBag.TotalPendingPrincipal = sdtoLoanRepayments.OrderByDescending(x => x.LoanRepaymentId).FirstOrDefault().PendingPrincipalAmount;
            }
            sdtoViewLoanRepayments viewItem = new sdtoViewLoanRepayments() { LoanId = LoanId, Repayments = sdtoLoanRepayments };
            return View(viewItem);
        }