public IActionResult ViewBallot(string ballotname) { var ballotQuery = _Context .Ballot .Where(b => b.BallotName == ballotname) .Include(b => b.Cadidates) .Include(b => b.Voter); var completeVoters = ballotQuery .SelectMany(b => b.Voter) .Select(v => v.Voter); var ballot = ballotQuery.FirstOrDefault(); if (ballot == null) { return(BadRequest("Not a valid ballot name")); } //_Context.Entry(ballot).Collection(b => b.Cadidates).Load(); //_Context.Entry(ballot).Collection(b => b.Voter).Load(); //_Context.Entry(ballot).Reference(b => b.Office).Load(); /*List<ApplicationUser> completeVoters = _Context * .Ballot * .Where(b => b.BallotName == ballotname) * .SelectMany(b => b.Voter) * .Select(vvb => vvb.Voter) * .ToList();*/ var location = ballot.ZipCode?.ToString() ?? ballot.DistrictName ?? ballot.RegionName; IIncludableQueryable <ApplicationUser, VoterDemographicsDataModel> allVoters = null; if (location == ballot.ZipCode.ToString()) { allVoters = _Context .Zip .Where(z => z.ZipCode == ballot.ZipCode) .SelectMany(z => z.Residents) .Select(r => r.User) .Include(u => u.Demographics); } else if (location == ballot.DistrictName) { allVoters = _Context .District .Where(d => d.DistrictName == ballot.DistrictName) .SelectMany(d => d.Zip) .Select(zfd => zfd.Zip) .SelectMany(z => z.Residents) .Select(r => r.User) .Include(u => u.Demographics); } else if (location == ballot.RegionName) { allVoters = _Context .Region .Where(r => r.RegionName == ballot.RegionName) .SelectMany(r => r.District) .Select(dfr => dfr.District) .SelectMany(d => d.Zip) .Select(zfd => zfd.Zip) .SelectMany(z => z.Residents) .Select(r => r.User) .Include(u => u.Demographics); } /*foreach (ApplicationUser user in allVoters) * { * _Context.Entry(user).Reference(u => u.Demographics).Load(); * }*/ int totalComplete = completeVoters.Count(); int totalUsers = allVoters?.Count() ?? 0; var ethnicityPercent = new Dictionary <string, float>(); var perCandidatePercent = new Dictionary <string, float>(); var incomePercent = new Dictionary <string, float>(); var partyPercent = new Dictionary <string, float>(); var readinessPercent = new Dictionary <string, float>(); var sexPercent = new Dictionary <string, float>(); var userDemographics = allVoters.Select(u => u.Demographics); foreach (VoterDemographicsDataModel demographics in userDemographics) { if (demographics == null) { continue; } if (ethnicityPercent.ContainsKey(demographics.Ethnicity)) { ethnicityPercent[demographics.Ethnicity] += (float)(100.0 / totalUsers); } else { ethnicityPercent[demographics.Ethnicity] = (float)(100.0 / totalUsers); } if (incomePercent.ContainsKey(demographics.IncomeRange)) { incomePercent[demographics.IncomeRange] += (float)(100.0 / totalUsers); } else { incomePercent[demographics.IncomeRange] = (float)(100.0 / totalUsers); } if (partyPercent.ContainsKey(demographics.Party)) { partyPercent[demographics.Party] += (float)(100.0 / totalUsers); } else { partyPercent[demographics.Party] = (float)(100.0 / totalUsers); } if (readinessPercent.ContainsKey(demographics.VoterReadiness)) { readinessPercent[demographics.VoterReadiness] += (float)(100.0 / totalUsers); } else { readinessPercent[demographics.VoterReadiness] = (float)(100.0 / totalUsers); } if (sexPercent.ContainsKey(demographics.Sex)) { sexPercent[demographics.Sex] += (float)(100.0 / totalUsers); } else { sexPercent[demographics.Sex] = (float)(100.0 / totalUsers); } } var votes = ballotQuery .SelectMany(b => b.Cadidates) .Select(c => new { c.User.UserName, c.VoteReceived.Count }); foreach (var candidateVotes in votes) { perCandidatePercent[candidateVotes.UserName] = (float)((candidateVotes.Count * 100.0) / (float)totalComplete); } ViewBallotViewModel model = new ViewBallotViewModel() { BallotName = ballotname, ElectionDay = ballot.ElectionDay, CompleteVotePercent = totalUsers == 0 ? 1 : (float)totalComplete / (float)totalUsers, PerCandidatePercent = perCandidatePercent, EthnicityPercent = ethnicityPercent, IncomePercent = incomePercent, PartyPercent = partyPercent, ReadinessPercent = readinessPercent, SexPercent = sexPercent }; return(View(model)); }