コード例 #1
0
        //
        // Display Nutrient Values in a formattable way
        public List<NutrientValue> DisplayNutrientList(User user, NutrientAmount nutrientAmount)
        {
            var userNutrients = new HashSet<int>(user.Nutrients.Select(c => c.NutrientID));

            var viewModel = new List<NutrientValue>();

            Dictionary<int, decimal> groupInfo = new Dictionary<int, decimal>();

            Dictionary<int, decimal> nutrientAmounts = DictionaryUnSerializer(nutrientAmount.NutrientAmounts);

            foreach (var groupDatum in db.GroupData.Where(g => g.GroupID == user.GroupID))
            {
                groupInfo.Add(groupDatum.NutrientID, groupDatum.RDA);
            }

            decimal amount;
            decimal amountPercentage;

            foreach (var nutrient in db.Nutrients)
            {
                if (nutrientAmounts.ContainsKey(nutrient.NutrientID))
                {
                    var measurement = nutrient.Measurement;
                    if (nutrient.Type == "v") { measurement = "%"; }

                    amount = 0;
                    amountPercentage = 0;

                    if (nutrientAmounts.ContainsKey(nutrient.NutrientID))
                    {
                        if (nutrient.Type == "v")
                        {
                            amount = Math.Round(nutrientAmounts[nutrient.NutrientID] / groupInfo[nutrient.NutrientID] * 100);
                            amountPercentage = 0;
                        }
                        else
                        {
                            amount = nutrientAmounts[nutrient.NutrientID];
                            if (groupInfo[nutrient.NutrientID] != 0) { amountPercentage = Math.Round(nutrientAmounts[nutrient.NutrientID] / groupInfo[nutrient.NutrientID] * 100); }
                            else { amountPercentage = 0; }
                        }
                    }

                    viewModel.Add(new NutrientValue
                    {
                        NutrientID = nutrient.NutrientID,
                        Name = nutrient.Name,
                        Amount = amount,
                        Measurement = measurement,
                        AmountPercentage = amountPercentage
                    });
                }
            }

            return viewModel;
        }
コード例 #2
0
        public List<NutrientValue> EditNutrientList(User user, NutrientAmount nutrientAmount)
        {
            var userNutrients = new HashSet<int>(user.Nutrients.Select(c => c.NutrientID));

            var viewModel = new List<NutrientValue>();

            Dictionary<int, decimal> nutrientAmounts = DictionaryUnSerializer(nutrientAmount.NutrientAmounts);

            foreach (var nutrient in db.Nutrients)
            {
                if (userNutrients.Contains(nutrient.NutrientID))
                {
                    var measurement = nutrient.Measurement;
                    if (nutrient.Type == "v") { measurement = "%"; }

                    decimal amount = 0;
                    if (nutrientAmounts.ContainsKey(nutrient.NutrientID))
                    {
                        if (nutrient.Type == "v") { amount = nutrientAmounts[nutrient.NutrientID] / nutrient.PercentageMultiplier; }
                        else { amount = nutrientAmounts[nutrient.NutrientID]; }
                    }

                    viewModel.Add(new NutrientValue
                    {
                        NutrientID = nutrient.NutrientID,
                        Name = nutrient.Name,
                        Amount = amount,
                        Measurement = measurement
                    });
                }
            }

            return viewModel;
        }