public NewShoppingList CreateShoppingList(List <Foods.NewFood> x, int consumers, string userId, string lang) { NewShoppingList shoppingList = new NewShoppingList(); shoppingList.response = new Global.Response(); //Stopwatch stopwatch = new Stopwatch(); //stopwatch.Start(); //shoppingList.time = 0; try { List <FoodQty> fq = LoadFoodQty(); List <Foods.NewFood> list = new List <Foods.NewFood>(); Foods f = new Foods(); List <Foods.NewFood> foods = new List <Foods.NewFood>(); if (consumers >= 1) { foods = f.MultipleConsumers(x, consumers); } else { foods = x; } Prices P = new Prices(); var prices = P.LoadData(userId, null); foreach (var g in foods) { var basicF = GetBasicFoodData(g.id, lang); basicF.price = prices.Find(a => a.food.title == basicF.basicFood); if (!string.IsNullOrWhiteSpace(basicF.basicFood)) { g.food = basicF.basicFood; } if (basicF.price != null) { g.price.currency = basicF.price.netPrice.currency; g.price.value = basicF.price.netPrice.value * g.mass / basicF.price.mass.value; } } var group = foods.GroupBy(a => a.food).Select(a => new GroupedFood { id = a.Select(i => i.id).FirstOrDefault(), food = a.Key, qty = a.Sum(q => q.quantity), unit = f.GetUnit(a.Sum(q => q.quantity), a.Select(u => u.unit).FirstOrDefault()), mass = Math.Round(a.Sum(m => m.mass), 0), smartQty = SmartQty(a.Select(i => i.id).FirstOrDefault() , a.Sum(q => q.quantity) , f.GetUnit(a.Sum(q => q.quantity), a.Select(u => u.unit).FirstOrDefault()) , Math.Round(a.Sum(m => m.mass), 0) , fq , lang), smartMass = SmartMass(Math.Round(a.Sum(m => m.mass), 0), lang), price = Math.Round(a.Sum(p => p.price.value), 2), currency = a.Select(u => u.price.currency).FirstOrDefault(), foodGroup = a.Select(i => i.foodGroup).FirstOrDefault() }).OrderBy(a => a.food).ToList(); var totalPrice = Math.Round(foods.Sum(a => a.price.value), 2); string currency = null; if (foods.Any(a => a.price.currency != null)) { currency = foods.FirstOrDefault(a => a.price.currency != null).price.currency; } List <Foods.FoodGroup> foodGroups = new List <Foods.FoodGroup>(); Foods F = new Foods(); using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + Server.MapPath("~/App_Data/" + dataBase))) { connection.Open(); foodGroups = F.GetFoodGroups(connection); } shoppingList.groupedFoodsByFoodGroup = new List <GroupedFoodsByFoodGroup>(); foreach (var fg in foodGroups) { GroupedFoodsByFoodGroup groupedFoodsByFoodGroup = new GroupedFoodsByFoodGroup(); groupedFoodsByFoodGroup.foodGroup = GetMainFoodGroupName(fg, foodGroups, lang); groupedFoodsByFoodGroup.foods = new List <GroupedFood>(); foreach (var g in group) { if (g.foodGroup.parent != "A") { if (g.foodGroup.parent == fg.group.code) { groupedFoodsByFoodGroup.foods.Add(g); } } else { if (g.foodGroup.code == fg.group.code) { groupedFoodsByFoodGroup.foods.Add(g); } } } if (groupedFoodsByFoodGroup.foods.Count > 0) { shoppingList.groupedFoodsByFoodGroup.Add(groupedFoodsByFoodGroup); } } shoppingList.total = new Total(); shoppingList.total.price = totalPrice; shoppingList.total.currency = currency; shoppingList.response.isSuccess = true; shoppingList.response.msg = "ok"; //shoppingList.time = stopwatch.Elapsed.TotalSeconds; return(shoppingList); } catch (Exception e) { L.SendErrorLog(e, x[0].id, null, "ShoppingList", "CreateShoppingList"); shoppingList.response.isSuccess = false; shoppingList.response.msg = e.Message; return(shoppingList); } }