public List <ShipmentDTO> GetBySelectedOrderIds(string orderIds) { var shipmentDTOs = new List <ShipmentDTO>(); var shipmentDTO = new ShipmentDTO { Orders = new List <ShipmentOrderDTO>() }; var shipments = _shipmentRepository.GetBySelectedOrderIds(orderIds); var currentAddress = ""; var currentCategoryId = 0; foreach (var shipment in shipments) { var currentOrder = new ShipmentOrderDTO { OrderId = Guid.NewGuid(), Address = shipment.Address, City = shipment.City, State = shipment.State, Country = shipment.Country, FirstName = shipment.FirstName, LastName = shipment.LastName, ProductId = shipment.ProductId, ProductName = shipment.ProductName, ProductDescription = shipment.ProductDescription, SKU = shipment.ProductSKU, Quantity = shipment.Quantity, Price = string.Format("{0:0.00#}", shipment.Price), Total = string.Format("{0:0.00#}", shipment.Total), CategoryId = shipment.CategoryId, CategoryName = shipment.CategoryName }; if (shipment.Address != currentAddress || shipment.CategoryId != currentCategoryId) { if (shipment != shipments.First()) { shipmentDTOs.Add(shipmentDTO); shipmentDTO = new ShipmentDTO { Orders = new List <ShipmentOrderDTO>() }; } shipmentDTO.Orders.Add(currentOrder); currentAddress = shipment.Address; currentCategoryId = shipment.CategoryId; } else { shipmentDTO.Orders.Add(currentOrder); } if (shipment == shipments.Last()) { shipmentDTOs.Add(shipmentDTO); } } return(shipmentDTOs); }