Пример #1
0
        public static List <Charity> GetCharitiesInCountry(string country)
        {
            List <Charity> result = new List <Charity>();

            CharityDonationOption.getDefault().Where(x => x.country == country).GroupBy(z => new { z.charityName, z.charityWebsite, z.iconUrl, z.description }).ToList().ForEach(y => result.Add(new Charity()
            {
                charityName    = y.Key.charityName,
                charityWebsite = y.Key.charityWebsite,
                iconUrl        = y.Key.iconUrl,
                description    = y.Key.description,
            }));
            return(result);
        }
Пример #2
0
        public static List <DonationImpact> GetDonationImpact(string country, string charity, double amount)
        {
            List <DonationImpact> result = new List <DonationImpact>();

            if (amount > 1000000)
            {
                amount = 1000000;
            }
            var options = CharityDonationOption.getDefault().Where(x => x.country == country && x.charityName == charity).ToList();

            var split = new List <KnapsackItem>();

            foreach (var typeD in options)
            {
                double currentSpend = 0;
                double valueMult    = 1;
                while (currentSpend < amount)
                {
                    split.Add(new KnapsackItem
                    {
                        parent = typeD,
                        value  = (double)typeD.ValuePerTreatment * valueMult,
                        size   = (double)typeD.costPerTreatment,
                    });
                    currentSpend += (double)typeD.costPerTreatment;
                    valueMult    *= typeD.nextEffectiveness;
                }
            }

            var spread = SplitBestEffect(amount, split).GroupBy(x => x.parent).Select(g => new
            {
                parent = g.Key,
                value  = g.Sum(x => x.value),
                size   = g.Sum(x => x.size),
                amt    = g.Count(),
            }).ToList();

            List <DonationImpactItem> resultTemp = new List <DonationImpactItem>();

            spread.ForEach(item =>
                           resultTemp.Add(new DonationImpactItem
            {
                locationName   = item.parent.locationName,
                latitude       = item.parent.latitude,
                longitude      = item.parent.longitude,
                peopleEffected = item.amt * item.parent.peopleEffected,
                amountOfItems  = item.amt,
                donation       = (decimal)item.size,
                type           = item.parent.treatmentName,
            })
                           );

            resultTemp.GroupBy(z => new
            {
                z.locationName,
                z.longitude,
                z.latitude
            }).ToList().ForEach(r => result.Add(new DonationImpact
            {
                locationName   = r.Key.locationName,
                longitude      = r.Key.longitude,
                latitude       = r.Key.latitude,
                peopleEffected = r.Sum(x => x.peopleEffected),
                subItems       = new List <DonationImpactItem>()
            }));

            resultTemp.ForEach(x => result.First(t => t.locationName == x.locationName).subItems.Add(x));

            return(result);
        }