public static List <BO_UnpaidCreditCustomerInvoice> GetUnPaidCreditCustomerInvoices(int custID) { using (AprosysAccountingEntities db_aa = new AprosysAccountingEntities()) { var lst = db_aa.Credit_GetUnPaidCustomersInvoice(custID); List <BO_UnpaidCreditCustomerInvoice> lst_cust = new List <BO_UnpaidCreditCustomerInvoice>(); BO_UnpaidCreditCustomerInvoice obj; var salePersons = GetSalePersonNameById(custID); var partialPayments = GetAllPartialPayments().GroupBy(x => x.InvoiceNum).ToDictionary(x => x.Key); foreach (var _creditCustomer in lst.ToList()) { obj = new BO_UnpaidCreditCustomerInvoice(); obj.invoiceNo = _creditCustomer.InvoiceNo; obj.salePerson = salePersons[custID]; obj.sellDate = _creditCustomer.SalesDate.Value; obj.netAmount = _creditCustomer.Amount ?? 0; obj.paidAmount = 0; obj.invoiceAmount = obj.netAmount; if (partialPayments.ContainsKey(_creditCustomer.InvoiceNo)) { obj.paidAmount = partialPayments[_creditCustomer.InvoiceNo].Sum(x => x.Amount); obj.netAmount = obj.invoiceAmount - obj.paidAmount; } if (obj.invoiceAmount > obj.paidAmount) { lst_cust.Add(obj); } } return(lst_cust.OrderByDescending(x => (Convert.ToInt32(x.invoiceNo.Remove(0, 4)))).ToList()); } }