public IHttpActionResult GetBills(string supplierId, string billNumber, DateTime?billDate, DateTime?dueDate, int?page, int?count) { try { using (MaxMasterDbEntities db = new MaxMasterDbEntities()) { int totalCount = 0; var billsList = db.GetBills(supplierId, billNumber, billDate, dueDate, page, count).ToList(); var quantity = ""; List <BillModel> bills = new List <BillModel>(); for (int i = 0; i < billsList.Count(); i++) { BillModel bill = new BillModel(); bill.BillNumber = billsList[i].BillNumber; bill.BillDate = billsList[i].BillDate; bill.Supplier = billsList[i].Supplier; bill.DueDate = billsList[i].DueDate; bill.BillId = billsList[i].BillId; bill.BillAmount = billsList[i].BillAmount; bill.QuantityUpdated = billsList[i].QuantityAdded; bill.TotalQuantity = billsList[i].TotalQuantity; var items = db.ItemsBillMappings.Where(x => x.BillId == bill.BillId).Select(x => new { x.ItemsMaster.ItemName, x.Quantity, x.ItemsMaster.Units }).ToList(); string str = string.Empty; if (items.Count() > 0) { foreach (var item in items) { if (item.Units.ToUpper() == "NUMBER") { quantity = item.Quantity.ToString(); } else { quantity = "1"; } str = str + item.ItemName + "[No of Items : " + quantity + "]" + ","; } str = str.Remove(str.Length - 1); } bill.Items = str; bills.Add(bill); } if (billsList.Count > 0) { totalCount = (int)billsList.FirstOrDefault().TotalCount; } return(Content(HttpStatusCode.OK, new { bills, totalCount })); } } catch (Exception ex) { new Error().logAPIError(System.Reflection.MethodBase.GetCurrentMethod().Name, ex.ToString(), ex.StackTrace); return(Content(HttpStatusCode.InternalServerError, "An error occured, please try agin later")); } }