public StatisticsCalculator() { try { VotingDatabaseEntities db = new VotingDatabaseEntities(); DisallowedTries = db.Statistics.Select(x => x.DisallowedTries).ToArray()[0]; NullVotes = db.Voters.Count(x => string.IsNullOrEmpty(x.CandidateId.ToString())); ValidVotes = db.Voters.Count(x => !string.IsNullOrEmpty(x.CandidateId.ToString())); //Create left join of tables Voters and Candidates var leftOuterJoin = (from v in db.Voters join c in db.Candidates on v.CandidateId equals c.CandidateId into joined from c in joined.DefaultIfEmpty() select new { v, c }); //Create right join of tables Voters and Candidates var rightOuterJoin = (from c in db.Candidates join v in db.Voters on c.CandidateId equals v.CandidateId into joined from v in joined.DefaultIfEmpty() select new { v, c }); //Combine both joins to get full join var fullOuterJoin = leftOuterJoin.Union(rightOuterJoin); //From full join group by names to count occurences of each Candidate namesCounts = fullOuterJoin.Where(x => x.c.Name != null).GroupBy(x => x.c.Name) .Select(y => new { Name = y.Key, Count = y.Where(z => z.v.CandidateId != null).Count() }).ToDictionary(t => t.Name, t => t.Count); //From full join group by parties to count occurences of each Party partiesCounts = fullOuterJoin.Where(x => x.c.Party != null).GroupBy(x => x.c.Party) .Select(y => new { Name = y.Key, Count = y.Where(z => z.v.CandidateId != null).Count() }).ToDictionary(t => t.Name, t => t.Count); } catch { MessageBox.Show("Could not provide voting results! Try again.", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error); } }
//Sends data about candidated to the database public void CandidateListToDatabase() { VotingDatabaseEntities db = new VotingDatabaseEntities(); foreach (Candidate candidate in Candidates) { db.Candidates.Add(candidate); } db.SaveChanges(); }
private void btnVote_Click(object sender, RoutedEventArgs e) { //Present the user with a messagebox to ensure his decision var mb = MessageBox.Show("Are you sure this the person you want to vote for?", "Are you sure?", MessageBoxButton.YesNo, MessageBoxImage.Question); if (mb == MessageBoxResult.Yes) { try { //If affirmative, find checked candidate List <Candidate> checkedCandidatesList = candidateData.Candidates.Where(x => x.IsChecked).ToList(); VotingDatabaseEntities db = new VotingDatabaseEntities(); //Encrypt the users personal id number LoggedVoter.PersonalIdNumber = Encrypter.EncryptString(LoggedVoter.PersonalIdNumber); //If single candidate was selected if (checkedCandidatesList.Count == 1) { //Add which candidate was selected LoggedVoter.CandidateId = checkedCandidatesList[0].CandidateId; } else { LoggedVoter.CandidateId = null; } //Update database db.Voters.Add(LoggedVoter); db.SaveChanges(); //Navigate user to the summary page SummaryPage summaryPage = new SummaryPage(); NavigationService.Navigate(summaryPage); } catch { MessageBox.Show("Could not save your vote! Try again.", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error); } } }
//Downloads data about candidates from the database public void CandidateListFromDatabase() { VotingDatabaseEntities db = new VotingDatabaseEntities(); Candidates = db.Candidates.Select(x => x).ToList(); }