Beispiel #1
0
        public ProductDTO2 GetBestSellingProduct(string country)
        {
            var productServices     = new ProductServices();
            var orderDetailServices = new OrderDetailServices();

            var productList     = productServices.GetAllProducts();
            var orderDetailList = orderDetailServices.GetAllOrderDetails();


            var orderGroup = _orderRepository.Set().GroupBy(o => o.Customer.Country);

            var ordersInCountry = new List <Order>();

            foreach (var group in orderGroup)
            {
                foreach (var order in group)
                {
                    if (group.Key == country)
                    {
                        ordersInCountry.Add(order);
                    }
                }
            }

            foreach (var o in ordersInCountry)
            {
                foreach (var od in o.Order_Details)
                {
                    foreach (var p in productList)
                    {
                        if (p.ProductID == od.ProductID)
                        {
                            p.TotalQuantity += od.Quantity;
                        }
                    }
                }
            }

            decimal currentValue;
            decimal maxValue = 0;

            ProductDTO2 BestSellingProduct = null;

            foreach (var p in productList)
            {
                currentValue = p.TotalQuantity;
                if (currentValue > maxValue)
                {
                    maxValue           = currentValue;
                    BestSellingProduct = p;
                }
            }

            return(BestSellingProduct);
        }