예제 #1
0
        private decimal?GetQuantity(DateTime date, int selectedId, string UserId)
        {
            Day            day      = _foodItemServices.GetDay(date, UserId);
            List <Product> products = _productServices.GetProducts(UserId, day).ToList();

            var nutrientResults = new List <decimal?>();

            // Make a big list of all the fooditems in the day.
            List <FoodItem> foodItems = new List <FoodItem>();

            foodItems.AddRange(day.Food);

            // Now group the fooditems to get rid of repeats.
            var groupedFoodItems = foodItems.
                                   GroupBy(f => f.Code).
                                   Select(fg => new { Code = fg.Key, Total = fg.Sum(f => f.Quantity) }).ToList();

            foreach (var g in groupedFoodItems)
            {
                if (Macronutrients.Nutrient(selectedId).Name.ToLower() == "alcohol")
                {
                    nutrientResults.Add(GetTotalAlcoholUnits(g.Total, products.Where(p => p.Code == g.Code).First()));
                }
                else
                {
                    nutrientResults.Add(GetTotalMacroNutrient(g.Total, products.Where(p => p.Code == g.Code).First(), Macronutrients.Nutrient(selectedId).Name));
                }
            }

            return(nutrientResults.Sum());
        }