private void BalanceConstituentCounts() { var RedConstituents = Constituents.Where((c) => c.party == Constituent.Party.Red); var BlueConstituents = Constituents.Where((c) => c.party == Constituent.Party.Blue); var RedCount = RedConstituents.Count(); var BlueCount = BlueConstituents.Count(); HashSet <District> changedDistricts = new HashSet <District>(); if (RedCount > BlueConstituents.Count()) { //find the disparity, and convert half that many to blue int diff = RedCount - BlueCount; //if diff is odd, convert one red to brown if ((diff % 2) == 1) { Constituent c = Utils.ChooseRandom(RedConstituents.ToList()); c.party = Constituent.Party.Yellow; changedDistricts.Add(c.District); } //choose enough constituents randomly to even things out, and convert them to the other party foreach (Constituent c in Utils.ChooseKRandom(RedConstituents.ToList(), diff / 2)) { c.party = Constituent.Party.Blue; changedDistricts.Add(c.District); } } else if (RedCount < BlueCount) { //find the disparity, and convert half that many to red int diff = BlueCount - RedCount; //if diff is odd, convert one red to brown if ((diff % 2) == 1) { Constituent c = Utils.ChooseRandom(RedConstituents.ToList()); c.party = Constituent.Party.Yellow; changedDistricts.Add(c.District); } //choose enough constituents randomly to even things out, and convert them to the other party foreach (Constituent c in Utils.ChooseKRandom(BlueConstituents.ToList(), diff / 2)) { c.party = Constituent.Party.Red; changedDistricts.Add(c.District); } } //update the member counts of every district we changed foreach (District d in changedDistricts) { d.UpdateMemberData(); } }
public ActionResult GetConstituent(string text) { Thread.Sleep(1000); var uriString = string.Format(serviceBaseUri + @"/Search?firstName={0}&lastName={0}&email={1}&phone={1}&occupationName={1}&occupationDescription={1}&instituteName={1}&instituteLocation={1}&qualification={1}&yearOfGradutation={1}&address={1}&state={1}&city={1}&country={1}&postcode={1}&preferedName={0}&houseName={1}&branch={1}", text,null); var constituentsData = HttpHelper.Get<ConstituentsData>(uriString); mapper = new AutoDataContractMapper(); var constituents = new Constituents(); mapper.MapList(constituentsData, constituents, typeof(Constituent)); IEnumerable<Constituent> enumerable = null; if (text.HasValue()) { enumerable = constituents.Where((p) => p.Name.FirstName.StartsWith(text,true,null) || p.Name.LastName.StartsWith(text,true,null)); } IEnumerable<SelectListItem> selectList = from c in enumerable select new SelectListItem { Text = c.Name.NameWithoutSalutation, Value = c.Id.ToString() }; return new JsonResult { Data = selectList }; }