public string GetWeeklyMenusTotals(string userId, List <string> menuList) { try { Foods f = new Foods(); List <Foods.Totals> xx = new List <Foods.Totals>(); List <List <Foods.MealsTotal> > lmt = new List <List <Foods.MealsTotal> >(); List <Foods.MealsTotal> mt_ = new List <Foods.MealsTotal>(); foreach (string menu in menuList) { if (!string.IsNullOrEmpty(menu)) { Menues m = new Menues(); Menues.NewMenu nm = new Menues.NewMenu(); nm = m.WeeklyMenu(userId, menu); Foods.Totals t = new Foods.Totals(); t = f.GetTotals_(nm.data.selectedFoods, nm.data.meals); xx.Add(t); List <Foods.MealsTotal> mt = new List <Foods.MealsTotal>(); mt = f.GetMealsTotal(nm.data.selectedFoods, nm.data.meals); lmt.Add(mt); } } foreach (List <Foods.MealsTotal> l in lmt) { foreach (Foods.MealsTotal o in l) { mt_.Add(o); } } List <Foods.MealsTotal> distMeal = mt_.GroupBy(a => a.code).Select(b => b.First()).ToList(); List <Foods.MealsTotal> zz = new List <Foods.MealsTotal>(); foreach (var ii in distMeal) { Foods.MealsTotal z = new Foods.MealsTotal(); z.code = ii.code; z.title = ii.title; List <Foods.MealsTotal> fmt_energy = mt_.Where(a => a.code == ii.code).ToList(); z.energy.val = fmt_energy.Count() > 0 ? Math.Round(fmt_energy.Average(a => a.energy.val), 1) : 0; List <Foods.MealsTotal> fmt_energyPerc = mt_.Where(a => a.code == ii.code).ToList(); z.energy.perc = fmt_energyPerc.Count() > 0 ? Math.Round(fmt_energyPerc.Average(a => a.energy.perc), 1) : 0; List <Foods.MealsTotal> fmt_carbohydrates = mt_.Where(a => a.code == ii.code).ToList(); z.carbohydrates.val = fmt_carbohydrates.Count() > 0 ? Math.Round(fmt_carbohydrates.Average(a => a.carbohydrates.val), 1) : 0; List <Foods.MealsTotal> fmt_carbohydratesPerc = mt_.Where(a => a.code == ii.code).ToList(); z.carbohydrates.perc = fmt_carbohydratesPerc.Count() > 0 ? Math.Round(fmt_carbohydratesPerc.Average(a => a.carbohydrates.perc), 1) : 0; List <Foods.MealsTotal> fmt_proteins = mt_.Where(a => a.code == ii.code).ToList(); z.proteins.val = fmt_proteins.Count() > 0 ? Math.Round(fmt_proteins.Average(a => a.proteins.val), 1) : 0; List <Foods.MealsTotal> fmt_proteinsPerc = mt_.Where(a => a.code == ii.code).ToList(); z.proteins.perc = fmt_proteinsPerc.Count() > 0 ? Math.Round(fmt_proteinsPerc.Average(a => a.proteins.perc), 1) : 0; List <Foods.MealsTotal> fmt_fats = mt_.Where(a => a.code == ii.code).ToList(); z.fats.val = fmt_fats.Count() > 0 ? Math.Round(fmt_fats.Average(a => a.fats.val), 1) : 0; List <Foods.MealsTotal> fmt_fatsPerc = mt_.Where(a => a.code == ii.code).ToList(); z.fats.perc = fmt_fatsPerc.Count() > 0 ? Math.Round(fmt_fatsPerc.Average(a => a.fats.perc), 1) : 0; zz.Add(z); } Foods.Totals x = new Foods.Totals(); x.mass = f.SmartRound(xx.Average(a => a.mass)); x.energy = f.SmartRound(xx.Average(a => a.energy)); x.carbohydrates = f.SmartRound(xx.Average(a => a.carbohydrates)); x.carbohydratesPercentage = f.SmartRound(xx.Average(a => a.carbohydratesPercentage)); x.proteins = f.SmartRound(xx.Average(a => a.proteins)); x.proteinsPercentage = f.SmartRound(xx.Average(a => a.proteinsPercentage)); x.fats = f.SmartRound(xx.Average(a => a.fats)); x.fatsPercentage = f.SmartRound(xx.Average(a => a.fatsPercentage)); x.servings.cerealsServ = f.SmartRound(xx.Average(a => a.servings.cerealsServ)); x.servings.vegetablesServ = f.SmartRound(xx.Average(a => a.servings.vegetablesServ)); x.servings.fruitServ = f.SmartRound(xx.Average(a => a.servings.fruitServ)); x.servings.meatServ = f.SmartRound(xx.Average(a => a.servings.meatServ)); x.servings.milkServ = f.SmartRound(xx.Average(a => a.servings.milkServ)); x.servings.fatsServ = f.SmartRound(xx.Average(a => a.servings.fatsServ)); x.servings.otherFoodsServ = f.SmartRound(xx.Average(a => a.servings.otherFoodsServ)); x.servings.otherFoodsEnergy = f.SmartRound(xx.Average(a => a.servings.otherFoodsEnergy)); x.mealsTotal = zz; x.starch = f.SmartRound(xx.Average(a => a.starch)); x.totalSugar = f.SmartRound(xx.Average(a => a.totalSugar)); x.glucose = f.SmartRound(xx.Average(a => a.glucose)); x.fructose = f.SmartRound(xx.Average(a => a.fructose)); x.saccharose = f.SmartRound(xx.Average(a => a.saccharose)); x.maltose = f.SmartRound(xx.Average(a => a.maltose)); x.lactose = f.SmartRound(xx.Average(a => a.lactose)); x.fibers = f.SmartRound(xx.Average(a => a.fibers)); x.saturatedFats = f.SmartRound(xx.Average(a => a.saturatedFats)); x.monounsaturatedFats = f.SmartRound(xx.Average(a => a.monounsaturatedFats)); x.polyunsaturatedFats = f.SmartRound(xx.Average(a => a.polyunsaturatedFats)); x.trifluoroaceticAcid = f.SmartRound(xx.Average(a => a.trifluoroaceticAcid)); x.cholesterol = f.SmartRound(xx.Average(a => a.cholesterol)); x.sodium = f.SmartRound(xx.Average(a => a.sodium)); x.potassium = f.SmartRound(xx.Average(a => a.potassium)); x.calcium = f.SmartRound(xx.Average(a => a.calcium)); x.magnesium = f.SmartRound(xx.Average(a => a.magnesium)); x.phosphorus = f.SmartRound(xx.Average(a => a.phosphorus)); x.iron = f.SmartRound(xx.Average(a => a.iron)); x.copper = f.SmartRound(xx.Average(a => a.copper)); x.zinc = f.SmartRound(xx.Average(a => a.zinc)); x.chlorine = f.SmartRound(xx.Average(a => a.chlorine)); x.manganese = f.SmartRound(xx.Average(a => a.manganese)); x.selenium = f.SmartRound(xx.Average(a => a.selenium)); x.iodine = f.SmartRound(xx.Average(a => a.iodine)); x.retinol = f.SmartRound(xx.Average(a => a.retinol)); x.carotene = f.SmartRound(xx.Average(a => a.carotene)); x.vitaminD = f.SmartRound(xx.Average(a => a.vitaminD)); x.vitaminE = f.SmartRound(xx.Average(a => a.vitaminE)); x.vitaminB1 = f.SmartRound(xx.Average(a => a.vitaminB1)); x.vitaminB2 = f.SmartRound(xx.Average(a => a.vitaminB2)); x.vitaminB3 = f.SmartRound(xx.Average(a => a.vitaminB3)); x.vitaminB6 = f.SmartRound(xx.Average(a => a.vitaminB6)); x.vitaminB12 = f.SmartRound(xx.Average(a => a.vitaminB12)); x.folate = f.SmartRound(xx.Average(a => a.folate)); x.pantothenicAcid = f.SmartRound(xx.Average(a => a.pantothenicAcid)); x.biotin = f.SmartRound(xx.Average(a => a.biotin)); x.vitaminC = f.SmartRound(xx.Average(a => a.vitaminC)); x.vitaminK = f.SmartRound(xx.Average(a => a.vitaminK)); x.price.value = Math.Round(xx.Average(a => a.price.value), 2); return(JsonConvert.SerializeObject(x, Formatting.None)); } catch (Exception e) { return(JsonConvert.SerializeObject(e.Message, Formatting.None)); } }