public static ResponseViewModel <PageViewModel <OrderDetailsViewModel> > GetOrderDetails(SearchViewModel searchViewModel) { using (EcommerceEntities entities = new EcommerceEntities()) { ResponseViewModel <PageViewModel <OrderDetailsViewModel> > ViewModel = new ResponseViewModel <PageViewModel <OrderDetailsViewModel> >(); List <GetOrders_Result> getOrderResult = new List <GetOrders_Result>(); PageViewModel <OrderDetailsViewModel> pageViewModel = new PageViewModel <OrderDetailsViewModel>(); getOrderResult = entities.GetOrders(searchViewModel.pageIndex, searchViewModel.pageSize, searchViewModel.searchText == "" ? null : searchViewModel.searchText, searchViewModel.sortOrder, searchViewModel.sortColoumn).ToList(); List <OrderDetailsViewModel> orderDetails = new List <OrderDetailsViewModel>(); //pageViewModel.Data = getOrderResult; int i = 1; Boolean flag = false; foreach (var singleOrderDetail in getOrderResult) { OrderDetailsViewModel orderDetail = new OrderDetailsViewModel(); flag = false; if (i == 1) { CustomerViewModel customerViewModel = new CustomerViewModel() { firstName = singleOrderDetail.FirstName, lastName = singleOrderDetail.LastName, emailId = singleOrderDetail.EmailId, city = singleOrderDetail.City, street = singleOrderDetail.Street, state = singleOrderDetail.State, zipCode = singleOrderDetail.ZipCode }; orderDetail.customerDetails = customerViewModel; orderDetail.GrandTotal = Convert.ToInt32(singleOrderDetail.GrandTotal); orderDetail.orderStatus = singleOrderDetail.OrderStatusName; orderDetail.orderId = Convert.ToInt32(singleOrderDetail.ID); orderDetail.Date = singleOrderDetail.Date; OrderItemViewModel orderItem = new OrderItemViewModel(); orderItem.itemName = singleOrderDetail.ItemName; orderItem.itemDiscount = Convert.ToInt32(singleOrderDetail.ItemDiscount); orderItem.itemImage = singleOrderDetail.ItemImage; orderItem.itemPrice = singleOrderDetail.ItemPrice.ToString(); orderItem.itemQuentity = singleOrderDetail.ItemQuetity.ToString(); orderItem.productId = 1; orderDetail.orderItemDetails = new List <OrderItemViewModel>(); orderDetail.orderItemDetails.Add(orderItem); orderDetails.Add(orderDetail); } else { foreach (var data in orderDetails) { if (singleOrderDetail.ID == data.orderId) { OrderItemViewModel orderItem = new OrderItemViewModel(); orderItem.itemName = singleOrderDetail.ItemName; orderItem.itemDiscount = Convert.ToInt32(singleOrderDetail.ItemDiscount); orderItem.itemImage = singleOrderDetail.ItemImage; orderItem.itemPrice = singleOrderDetail.ItemPrice.ToString(); orderItem.itemQuentity = singleOrderDetail.ItemQuetity.ToString(); data.orderItemDetails.Add(orderItem); flag = true; } } if (flag != true) { CustomerViewModel customerViewModel = new CustomerViewModel() { firstName = singleOrderDetail.FirstName, lastName = singleOrderDetail.LastName, emailId = singleOrderDetail.EmailId, city = singleOrderDetail.City, street = singleOrderDetail.Street, state = singleOrderDetail.State, zipCode = singleOrderDetail.ZipCode }; orderDetail.customerDetails = customerViewModel; orderDetail.GrandTotal = Convert.ToInt32(singleOrderDetail.GrandTotal); orderDetail.orderStatus = singleOrderDetail.OrderStatusName; orderDetail.orderId = Convert.ToInt32(singleOrderDetail.ID); orderDetail.Date = singleOrderDetail.Date; OrderItemViewModel orderItem = new OrderItemViewModel(); orderItem.itemName = singleOrderDetail.ItemName; orderItem.itemDiscount = Convert.ToInt32(singleOrderDetail.ItemDiscount); orderItem.itemImage = singleOrderDetail.ItemImage; orderItem.itemPrice = singleOrderDetail.ItemPrice.ToString(); orderItem.itemQuentity = singleOrderDetail.ItemQuetity.ToString(); orderDetail.orderItemDetails = new List <OrderItemViewModel>(); orderDetail.orderItemDetails.Add(orderItem); orderDetails.Add(orderDetail); } } i = i + 1; } pageViewModel.Data = orderDetails; pageViewModel.totalRecords = getOrderResult.Count(); if (((pageViewModel.totalRecords % searchViewModel.pageSize) == 0)) { if (pageViewModel.totalRecords < searchViewModel.pageSize) { pageViewModel.totalPage = 1; } else { pageViewModel.totalPage = (pageViewModel.totalRecords / searchViewModel.pageSize); } } else { pageViewModel.totalPage = (pageViewModel.totalRecords / 10) + 1; } ViewModel.Data = pageViewModel; return(ViewModel); } }