public static IEnumerable <Order> GetCustomerOrders(GetCustomerOrdersRequest request) { var orders = new List <Order>(); var orderStatus = ""; var orderType = ""; var orderid = 0; if (request.CustomerID == 0) { throw new ArgumentException("CustomerID is required."); } // Apply the request variables if (request.OrderStatuses.Length > 0) { orderStatus = string.Join(",", request.OrderStatuses.ToList()); } if (request.OrderTypes.Length > 0) { orderType = string.Join(",", request.OrderTypes.ToList()); } if (request.OrderID != null) { orderid = (int)request.OrderID; } using (var contextsql = Exigo.Sql()) { var sql = string.Format(@"Exec GetCustomerOrders {0}, {1}, {2}, {3}, '{4}', '{5}', '{6}'", request.CustomerID, orderid, request.Page, request.Take, orderStatus, orderType, request.StartDate); orders = contextsql.Query <Order>(sql).ToList(); } // If we don't have any orders, stop here. if (orders.Count == 0) { yield break; } orders = orders.ToList().Select(s => { var order = s; order.Customer = GetCustomer(s.CustomerID); return(order); }).ToList(); // Setup the base orders query if (request.IncludePayments) { orders = orders.ToList().Select(s => { var order = s; order.Payments = GetOrderPayments(s.OrderID).ToList(); return(order); }).ToList(); } // Get the order details if applicable if (request.IncludeOrderDetails) { orders = orders.ToList().Select(s => { var order = s; order.Details = GetOrderDetails(s.OrderID).ToList(); return(order); }).ToList(); //var itemIDs = orders.ToList().Select(.Select(c => c.ItemID).Distinct().ToList(); // Get a unique list of item IDs in the orders var itemIDs = orders.ToList().Select(o => o.Details.Select(od => { return(od.ItemCode); }).Distinct() ).ToList(); var apiItems = new List <Item>(); // Get the extra data we need for each detail if (itemIDs.Count > 0) { foreach (var itemID in itemIDs) { foreach (var id in itemID) { apiItems.Add(GetItem(id)); } } } // Format the data to our models foreach (var order in orders) { // Get the order details var details = order.Details; foreach (var detail in details) { var apiItem = apiItems.Where(c => c.ItemCode == detail.ItemCode).FirstOrDefault(); if (apiItem != null) { detail.ImageUrl = apiItem.SmallImageUrl; detail.IsVirtual = apiItem.IsVirtual; } } order.Details = details; } } // Format the data to our models foreach (var order in orders) { yield return(order); } }
public static IEnumerable <Order> GetCustomerOrders(GetCustomerOrdersRequest request) { /* 20161222 80967 DV. Business rules have changed that will affect search. * 1. Remove filter using Other11 field. * 2. Instead of using ItemCode, use ItemID to identify PartnerStore and AffiliateStore items in an order, because BA and client believe that the ItemCode COULD change. * That is, if an order has ItemID 58 or 627 in the corresponding orderdetails table then simply filter out the entire order from the search results. * Note: For the tab called Partners & Affiliates, only include orders that contain ItemCode 58 or 627 specifically. * Filter out all orders using Order Type "Import" which is value 5. */ if (request.CustomerID == 0) { throw new ArgumentException("CustomerID is required."); } var context = Exigo.CreateODataContext <ExigoContext>(GlobalSettings.Exigo.Api.SandboxID); //Clear the OrderDetailModels if (OrderDetailModels != null) { OrderDetailModels = new List <ExigoService.OrderDetail>(); } var orders = new List <Order>(); // Setup the base orders query var ordersBaseQuery = context.Orders; if (request.IncludePayments) { ordersBaseQuery = ordersBaseQuery.Expand("Payments"); } var ordersQuery = ordersBaseQuery.Where(c => c.CustomerID == request.CustomerID); //20161222 80967 DV. Per item 3 of GetCustomerOrders summary, filter out all orders using Order Type "Import", which is value 5 ordersQuery.Where(c => c.OrderTypeID != 5); // Apply the request variables if (request.OrderID != null) { ordersQuery = ordersQuery.Where(c => c.OrderID == ((int)request.OrderID)); } if (request.OrderStatuses.Length > 0) { ordersQuery = ordersQuery.Where(request.OrderStatuses.ToList().ToOrExpression <Common.Api.ExigoOData.Order, int>("OrderStatusID")); } if (request.OrderTypes.Length > 0) { ordersQuery = ordersQuery.Where(request.OrderTypes.ToList().ToOrExpression <Common.Api.ExigoOData.Order, int>("OrderTypeID")); } if (request.StartDate != null) { ordersQuery = ordersQuery.Where(c => c.OrderDate >= (DateTime)request.StartDate); } if (request.EndDate != null) //20161212 80967 DV. Add ability for user to control both StartDate and EndDate { ordersQuery = ordersQuery.Where(c => c.OrderDate <= Convert.ToDateTime(request.EndDate).AddDays(1)); //20161216 #80967 DV. Like in commissions you need to add 1 to the end date in order to included all data up until 11:59:59:59 PM } //I.M. 11/29/2016 #80967 Add SQL query for grabbing all Order information // var orderSql = new List<ExigoService.Order>(); // try // { // using (var context = Exigo.Sql()) // { // orderSql = context.Query<ExigoService.Order>(@" // SELECT // o.OrderID, // o.OrderDate, // o.OrderStatusID, // o.OrderTypeID, // o.Total, // o.FirstName, // o.LastName, // o.Address1, // o.City, // o.State, // o.Zip, // o.Country, // o.Phone, // o.Email, // od.ItemDescription, // od.ItemCode // FROM // order o INNER JOIN orderdetails od // ON // o.OrderID = od.OrderID // WHERE // // ", new // { // OrderID = request.OrderID, // }).ToList(); // } // } // catch (Exception e) // { // Console.Write(e); // } // return orderSql; // } // Get the orders var odataOrders = ordersQuery .OrderByDescending(c => c.OrderDate) .Skip(request.Skip) //.Take(20) //.Take(request.Take) 20161226 80967 DV. .Select(c => c) .ToList(); //Begin flagged block. 20161230 DV. This block of code will be examined for performance analysis. // If we don't have any orders, stop here. if (odataOrders.Count == 0) { yield break; } // Collect our orders together foreach (var order in odataOrders) { var model = (Order)order; orders.Add(model); } // Get the order details if applicable if (request.IncludeOrderDetails) { // Get the order IDs var orderIDs = orders.Select(c => c.OrderID).Distinct().ToList(); // Get the order details (Results are saved via the ReadingEntity delegate to the private OrderDetailModels property. context.ReadingEntity += context_ReadingEntity; context.OrderDetails .Where(orderIDs.ToOrExpression <Common.Api.ExigoOData.OrderDetail, int>("OrderID")) .ToList(); // Get a unique list of item IDs in the orders var itemIDs = OrderDetailModels.Select(c => c.ItemID).Distinct().ToList(); // Get the extra data we need for each detail var apiItems = new List <Common.Api.ExigoOData.Item>(); if (itemIDs.Count > 0) { apiItems = context.Items .Where(itemIDs.ToOrExpression <Common.Api.ExigoOData.Item, int>("ItemID")) .Select(c => new Common.Api.ExigoOData.Item { ItemCode = c.ItemCode, SmallImageUrl = c.SmallImageUrl, IsVirtual = c.IsVirtual }) .ToList(); } // Format the data to our models foreach (var order in orders) { // Get the order details var details = OrderDetailModels.Where(c => c.OrderID == order.OrderID); foreach (var detail in details) { var apiItem = apiItems.Where(c => c.ItemCode == detail.ItemCode).FirstOrDefault(); if (apiItem != null) { detail.ImageUrl = apiItem.SmallImageUrl; detail.IsVirtual = apiItem.IsVirtual; } } order.Details = details; } } //End of flagged block. //20161222 80967 DV. Per item 2 of GetCustomerOrders summary, filter out any orderID's that has ItemID 58 or 627 in a corresponding orderdetail table foreach (var order in orders.ToList()) { var orderID = order.OrderID; var orderdetails = order.Details.Where(c => c.OrderID == order.OrderID); bool removecurrentorder = false; foreach (var item in orderdetails) { if (request.ShowOnlyPartnerAffiliateOrders) { if (!(item.ItemID == 58 || item.ItemID == 627)) { //orders.Remove(order); removecurrentorder = true; } } else if (request.ShowOnlyFeesAndServicesOrders) //20161229 80697 DV. Add filter to display orders with fees and services { if (!(item.ItemID == 45 || item.ItemID == 289 || item.ItemID == 290)) { //orders.Remove(order); removecurrentorder = true; } } else //Show all orders except orders that contain partner and affiliate items as well as fees and services { if (item.ItemID == 58 || item.ItemID == 627 || item.ItemID == 45 || item.ItemID == 289 || item.ItemID == 290) { orders.Remove(order); } } } if (removecurrentorder) { orders.Remove(order); //removecurrentorder = false; //reset bool } } // Format the data to our models foreach (var order in orders) { yield return(order); } }
public static IEnumerable <Order> GetCustomerOrders(GetCustomerOrdersRequest request) { var context = Exigo.OData(); // Clear the OrderDetailModels if (OrderDetailModels != null) { OrderDetailModels = new List <ExigoService.OrderDetail>(); } var orders = new List <Order>(); // Setup the base orders query var ordersBaseQuery = context.Orders; if (request.IncludePayments) { ordersBaseQuery = ordersBaseQuery.Expand("Payments"); } var ordersQuery = ordersBaseQuery.Where(c => c.CustomerID == request.CustomerID); // Apply the request variables if (request.OrderID != null) { ordersQuery = ordersQuery.Where(c => c.OrderID == ((int)request.OrderID)); } if (request.OrderStatuses.Length > 0) { ordersQuery = ordersQuery.Where(request.OrderStatuses.ToList().ToOrExpression <Common.Api.ExigoOData.Order, int>("OrderStatusID")); } if (request.OrderTypes.Length > 0) { ordersQuery = ordersQuery.Where(request.OrderTypes.ToList().ToOrExpression <Common.Api.ExigoOData.Order, int>("OrderTypeID")); } if (request.StartDate != null) { ordersQuery = ordersQuery.Where(c => c.OrderDate >= (DateTime)request.StartDate); } // Get the orders var odataOrders = ordersQuery .OrderByDescending(c => c.OrderDate) .Skip(request.Skip) .Take(request.Take) .Select(c => c) .ToList(); // If we don't have any orders, stop here. if (odataOrders.Count == 0) { yield break; } // Collect our orders together foreach (var order in odataOrders) { var model = (Order)order; orders.Add(model); } // Get the order details if applicable if (request.IncludeOrderDetails) { // Get the order IDs var orderIDs = orders.Select(c => c.OrderID).Distinct().ToList(); // Get the order details (Results are saved via the ReadingEntity delegate to the private OrderDetailModels property. context.ReadingEntity += context_ReadingEntity; context.OrderDetails .Where(orderIDs.ToOrExpression <Common.Api.ExigoOData.OrderDetail, int>("OrderID")) .ToList(); // Get a unique list of item IDs in the orders var itemIDs = OrderDetailModels.Select(c => c.ItemID).Distinct().ToList(); // Get the extra data we need for each detail var apiItems = new List <Common.Api.ExigoOData.Item>(); if (itemIDs.Count > 0) { apiItems = context.Items .Where(itemIDs.ToOrExpression <Common.Api.ExigoOData.Item, int>("ItemID")) .Select(c => new Common.Api.ExigoOData.Item { ItemCode = c.ItemCode, SmallImageUrl = c.SmallImageUrl, IsVirtual = c.IsVirtual }) .ToList(); } // Format the data to our models foreach (var order in orders) { // Get the order details var details = OrderDetailModels.Where(c => c.OrderID == order.OrderID); foreach (var detail in details) { var apiItem = apiItems.Where(c => c.ItemCode == detail.ItemCode).FirstOrDefault(); if (apiItem != null) { detail.ImageUrl = apiItem.SmallImageUrl; detail.IsVirtual = apiItem.IsVirtual; } } order.Details = details; } } // Format the data to our models foreach (var order in orders) { yield return(order); } }
/// <summary> /// SQL Get Orders call. Uses Web Service as a fail safe. /// </summary> /// <param name="request"></param> /// <returns></returns> public static GetCustomerOrdersResponse GetCustomerOrders_SQL(GetCustomerOrdersRequest request) { var orders = new List <Order>(); var model = new GetCustomerOrdersResponse(); try { if (request.OrderID != null && request.OrderID <= 0 && request.CustomerID <= 0) { throw new ArgumentOutOfRangeException("Customer ID and/or Order ID is required."); } var where = ""; if (request.OrderID != null) { where = "OrderID = @orderid AND CustomerID = @customerid"; } else { where = "CustomerID = @customerid"; } if (request.OrderTypes.Count() != 0) { where += " AND OrderTypeID in @ordertypes"; } if (request.OrderStatuses.Count() != 0) { where += " AND OrderStatusID in @orderstatuses"; } // We only populate the Count if we do not already have it provided to us var getCount = request.TotalRowCount == 0; var countQuery = ""; if (getCount) { countQuery = String.Format(@" SELECT Count(OrderID) FROM [Orders] Where {0} ", where); } else { model.OrderCount = request.TotalRowCount; } var query = String.Format(@" ;SELECT [OrderID] ,[CustomerID] ,[OrderStatusID] ,[OrderTypeID] ,[OrderDate] ,[CurrencyCode] ,[WarehouseID] ,[ShipMethodID] ,[PriceTypeID] ,[Notes] ,[Total] ,[SubTotal] ,[TaxTotal] ,[ShippingTotal] ,[DiscountTotal] ,[DiscountPercent] ,[WeightTotal] ,[BVTotal] = [BusinessVolumeTotal] ,[CVTotal] = [CommissionableVolumeTotal] ,[TrackingNumber1] ,[TrackingNumber2] ,[TrackingNumber3] ,[TrackingNumber4] ,[TrackingNumber5] ,[Other1Total] ,[Other2Total] ,[Other3Total] ,[Other4Total] ,[Other5Total] ,[Other6Total] ,[Other7Total] ,[Other8Total] ,[Other9Total] ,[Other10Total] ,[ShippingTax] ,[OrderTax] ,[FedTaxTotal] ,[StateTaxTotal] ,[FedShippingTax] ,[StateShippingTax] ,[CityShippingTax] ,[CityLocalShippingTax] ,[CountyShippingTax] ,[CountyLocalShippingTax] ,[Other11] ,[Other12] ,[Other13] ,[Other14] ,[Other15] ,[Other16] ,[Other17] ,[Other18] ,[Other19] ,[Other20] ,[IsCommissionable] ,[AutoOrderID] ,[ReturnOrderID] ,[ReplacementOrderID] ,[ParentOrderID] ,[BatchID] ,[DeclineCount] ,[TransferToCustomerID] ,[PartyID] ,[WebCarrierID1] ,[WebCarrierID2] ,[WebCarrierID3] ,[WebCarrierID4] ,[WebCarrierID5] ,[ShippedDate] ,[CreatedDate] ,[LockedDate] ,[ModifiedDate] ,[CreatedBy] ,[ModifiedBy] ,[SuppressPackSlipPrice] ,[ReturnCategoryID] ,[ReplacementCategoryID] ,[FirstName] ,[LastName] ,[Company] ,[Address1] ,[Address2] ,[City] ,[State] ,[Zip] ,[Country] ,[County] ,[Email] ,[Phone] FROM [Orders] Where {0} ORDER BY OrderDate DESC OFFSET @skip ROWS FETCH NEXT @take ROWS ONLY ", where); using (var context = Exigo.Sql()) { context.Open(); if (getCount) { var count = context.Query <int>(countQuery, new { orderid = request.OrderID, orderstatuses = request.OrderStatuses, ordertypes = request.OrderTypes, customerid = request.CustomerID }).FirstOrDefault(); model.OrderCount = count; } orders = context.Query <Order, ShippingAddress, Order>( query , (o, sa) => { o.Recipient = sa; return(o); } , param: new { orderid = request.OrderID, orderstatuses = request.OrderStatuses, ordertypes = request.OrderTypes, customerid = request.CustomerID, skip = request.Skip, take = request.Take } , splitOn: "FirstName" ).ToList(); } if (request.IncludeOrderDetails) { var orderDetails = new List <OrderDetail>(); using (var context = Exigo.Sql()) { List <int> orderIDs = orders.Select(order => order.OrderID).ToList(); orderDetails = context.Query <OrderDetail>(@" SELECT [OrderID] ,[OrderLine] ,[ItemID] ,[ItemCode] ,[ItemDescription] ,[Quantity] ,[PriceEach] ,[PriceTotal] ,[Tax] ,[WeightEach] ,[Weight] ,[BusinessVolumeEach] ,[BusinessVolume] ,[CommissionableVolumeEach] ,[CommissionableVolume] ,[ParentItemID] ,[Taxable] ,[FedTax] ,[StateTax] ,[CityTax] ,[CityLocalTax] ,[CountyTax] ,[CountyLocalTax] ,[ManualTax] ,[IsStateTaxOverride] ,[Reference1] FROM [OrderDetails] WHERE OrderID in @orderids; ", new { orderids = orderIDs }).ToList(); } // Temp List of Item that contains only the fields we need to add to the Order Detail items var imageUrls = new List <Item>(); List <string> detailItemCodes = new List <string>(); foreach (var order in orders) { foreach (var detail in orderDetails) { detailItemCodes.Add(detail.ItemCode); } } foreach (var detail in orderDetails) { detailItemCodes.Add(detail.ItemCode); } using (var context = Exigo.Sql()) { imageUrls = context.Query <Item>(@" SELECT DISTINCT [ItemCode] ,[SmallImageUrl] = [SmallImageName] ,[IsVirtual] FROM [Items] WHERE ItemCode IN @itemcodes ", new { itemcodes = detailItemCodes.Distinct().ToArray() }).ToList(); } foreach (var order in orders) { var details = orderDetails.Where(o => o.OrderID == order.OrderID); foreach (var detail in orderDetails) { var imageUrl = imageUrls.Where(i => i.ItemCode == detail.ItemCode).FirstOrDefault(); if (imageUrl != null) { detail.ImageUrl = imageUrl.SmallImageUrl; } } order.Details = details.ToList(); } } if (request.IncludePayments) { var orderPayments = new List <Payment>(); List <int> orderIDs = orders.Select(order => order.OrderID).ToList(); using (var context = Exigo.Sql()) { orderPayments = context.Query <Payment>(@" SELECT [PaymentID] ,[CustomerID] ,[OrderID] ,[PaymentTypeID] ,[PaymentDate] ,[Amount] ,[CurrencyCode] ,[WarehouseID] ,[BillingName] ,[CreditCardTypeID] ,[CreditCardNumber] ,[AuthorizationCode] ,[Memo] ,[BillingAddress1] ,[BillingAddress2] ,[BillingCity] ,[BillingState] ,[BillingZipAddress] ,[BillingCountry] FROM [Payments] WHERE OrderID in @orderids; ", new { orderids = orderIDs }).ToList(); } foreach (var order in orders) { var payment = orderPayments.Where(o => o.OrderID == order.OrderID); order.Payments = payment; } } } catch (Exception ex) { orders = GetCustomerOrders(request); } model.Orders = orders; return(model); }
/// <summary> /// Web Service Get Orders call /// </summary> /// <param name="request"></param> /// <returns></returns> public static List <Order> GetCustomerOrders(GetCustomerOrdersRequest request) { var orders = new List <Order>(); try { if (request.CustomerID == 0) { throw new ArgumentException("CustomerID is required."); } var orequest = new GetOrdersRequest(); // Apply the request variables orequest.CustomerID = request.CustomerID; orequest.BatchSize = 10000; if (request.OrderID != null) { orequest.OrderID = request.OrderID; } if (request.StartDate != null) { orequest.OrderDateStart = request.StartDate; } var response = Exigo.WebService().GetOrders(orequest); var responseOrders = new List <Order>(); response.Orders.ToList().ForEach(o => { var _order = (Order)o; var includeInOrderList = false; // Apply additional filters that are not available in our Get Orders Request if (request.OrderStatuses.Length > 0 || request.OrderTypes.Length > 0) { if (request.OrderStatuses.Length > 0 && request.OrderStatuses.Contains(_order.OrderStatusID)) { includeInOrderList = true; } if (request.OrderTypes.Length > 0 && request.OrderTypes.Contains(_order.OrderTypeID)) { includeInOrderList = true; } } else { includeInOrderList = true; } if (includeInOrderList) { if (request.IncludeOrderDetails) { // Go ahead and get our list of Item Images and details that do not come back with the API call var _orders = response.Orders.ToList(); var itemCodes = new List <string>(); foreach (var order in response.Orders) { foreach (var detail in order.Details) { itemCodes.Add(detail.ItemCode); } } // Temp List of Item that contains only the fields we need to add to the Order Detail items var additionalItemDetails = new List <Item>(); using (var context = Exigo.Sql()) { additionalItemDetails = context.Query <Item>(@" select distinct ItemCode, SmallImageUrl = @imageUrlPrefix + SmallImageName, IsVirtual from Items where ItemCode in @itemCodes ", new { itemCodes, imageUrlPrefix = GlobalUtilities.GetProductImagePath() }).ToList(); } var _details = new List <OrderDetail>(); o.Details.ToList().ForEach(d => { var detail = (OrderDetail)d; var additionalDetail = additionalItemDetails.FirstOrDefault(i => i.ItemCode == d.ItemCode); if (additionalDetail != null) { detail.ImageUrl = additionalDetail.SmallImageUrl; detail.IsVirtual = additionalDetail.IsVirtual; } _details.Add(detail); }); _order.Details = _details; } if (request.IncludePayments) { var _payments = new List <Payment>(); o.Payments.ToList().ForEach(p => { var payment = new Payment(); payment.Amount = p.Amount; payment.AuthorizationCode = p.AuthorizationCode; payment.BillingName = p.BillingName; payment.CreditCardNumber = p.CreditCardNumberDisplay; payment.CreditCardTypeID = p.CreditCardType; payment.CurrencyCode = p.CurrencyCode; payment.CustomerID = p.CustomerID; payment.Memo = p.Memo; payment.OrderID = Convert.ToInt32(p.OrderID); payment.PaymentDate = p.PaymentDate; payment.PaymentID = p.PaymentID; payment.PaymentTypeID = (int)p.PaymentType; _payments.Add(payment); }); _order.Payments = _payments; } responseOrders.Add(_order); } }); orders = responseOrders .OrderBy(c => c.OrderDate) .Skip(request.Skip) .Take(request.Take) .ToList(); } catch { return(null); } return(orders); }
public static IEnumerable<Order> GetCustomerOrders(GetCustomerOrdersRequest request) { if (request.CustomerID == 0) { throw new ArgumentException("CustomerID is required."); } var context = Exigo.CreateODataContext<ExigoContext>(GlobalSettings.Exigo.Api.SandboxID); // Clear the OrderDetailModels if (OrderDetailModels != null) OrderDetailModels = new List<ExigoService.OrderDetail>(); var orders = new List<Order>(); // Setup the base orders query var ordersBaseQuery = context.Orders; if (request.IncludePayments) ordersBaseQuery = ordersBaseQuery.Expand("Payments"); var ordersQuery = ordersBaseQuery.Where(c => c.CustomerID == request.CustomerID); // Apply the request variables if (request.OrderID != null) { ordersQuery = ordersQuery.Where(c => c.OrderID == ((int)request.OrderID)); } if (request.OrderStatuses.Length > 0) { ordersQuery = ordersQuery.Where(request.OrderStatuses.ToList().ToOrExpression<Common.Api.ExigoOData.Order, int>("OrderStatusID")); } if (request.OrderTypes.Length > 0) { ordersQuery = ordersQuery.Where(request.OrderTypes.ToList().ToOrExpression<Common.Api.ExigoOData.Order, int>("OrderTypeID")); } if (request.StartDate != null) { ordersQuery = ordersQuery.Where(c => c.OrderDate >= (DateTime)request.StartDate); } // Get the orders var odataOrders = ordersQuery .OrderByDescending(c => c.OrderDate) .Skip(request.Skip) .Take(request.Take) .Select(c => c) .ToList(); // If we don't have any orders, stop here. if (odataOrders.Count == 0) yield break; // Collect our orders together foreach (var order in odataOrders) { var model = (Order)order; orders.Add(model); } // Get the order details if applicable if (request.IncludeOrderDetails) { // Get the order IDs var orderIDs = orders.Select(c => c.OrderID).Distinct().ToList(); // Get the order details (Results are saved via the ReadingEntity delegate to the private OrderDetailModels property. context.ReadingEntity += context_ReadingEntity; context.OrderDetails .Where(orderIDs.ToOrExpression<Common.Api.ExigoOData.OrderDetail, int>("OrderID")) .ToList(); // Get a unique list of item IDs in the orders var itemIDs = OrderDetailModels.Select(c => c.ItemID).Distinct().ToList(); // Get the extra data we need for each detail var apiItems = new List<Common.Api.ExigoOData.Item>(); if (itemIDs.Count > 0) { apiItems = context.Items .Where(itemIDs.ToOrExpression<Common.Api.ExigoOData.Item, int>("ItemID")) .Select(c => new Common.Api.ExigoOData.Item { ItemCode = c.ItemCode, SmallImageUrl = c.SmallImageUrl, IsVirtual = c.IsVirtual }) .ToList(); } // Format the data to our models foreach (var order in orders) { // Get the order details var details = OrderDetailModels.Where(c => c.OrderID == order.OrderID); foreach (var detail in details) { var apiItem = apiItems.Where(c => c.ItemCode == detail.ItemCode).FirstOrDefault(); if (apiItem != null) { detail.ImageUrl = apiItem.SmallImageUrl; detail.IsVirtual = apiItem.IsVirtual; } } order.Details = details; } } // Format the data to our models foreach (var order in orders) { yield return order; } }