Пример #1
0
        private PollResultsViewModel CalculateNewResult(Poll poll)
        {
            PollResultsViewModel resultVm = new PollResultsViewModel();

            IEnumerable <PollVote> votes = pollDomainService.GetVotes(poll.Id);

            IEnumerable <KeyValuePair <Guid, int> > groupedVotes = from v in votes
                                                                   group v by v.PollOptionId into g
                                                                   select new KeyValuePair <Guid, int>(g.Key, g.Count());

            resultVm.TotalVotes = votes.Count();

            foreach (KeyValuePair <Guid, int> g in groupedVotes)
            {
                PollOptionResultsViewModel newOptionResult = new PollOptionResultsViewModel
                {
                    OptionId   = g.Key,
                    VoteCount  = g.Value,
                    Percentage = ((g.Value / (decimal)resultVm.TotalVotes) * 100).ToString("N2", new CultureInfo("en-us"))
                };

                resultVm.OptionResults.Add(newOptionResult);
            }

            return(resultVm);
        }