Esempio n. 1
0
        private IEnumerable <GroupItemViewModel> CreateGroupItemsFromResults(IEnumerable <Result> results)
        {
            List <GroupItemViewModel> groupItemsVM = new List <GroupItemViewModel>();

            foreach (var result in results)
            {
                if (!groupItemsVM.Exists(x => x.Team == result.HomeTeam))
                {
                    GroupItemViewModel item = new GroupItemViewModel(result.HomeTeam);
                    groupItemsVM.Add(item);
                }
                if (!groupItemsVM.Exists(x => x.Team == result.AwayTeam))
                {
                    GroupItemViewModel item = new GroupItemViewModel(result.AwayTeam);
                    groupItemsVM.Add(item);
                }
            }

            foreach (var item in groupItemsVM)
            {
                item.PlayedGames  = ResultsService.GetPlayedGames(item.Team, results);
                item.Goals        = ResultsService.GetGoals(item.Team, results);
                item.GoalsAgainst = ResultsService.GetGoalsAgainst(item.Team, results);
                item.Win          = ResultsService.GetWin(item.Team, results);
                item.Lose         = ResultsService.GetLose(item.Team, results);
                item.Draw         = ResultsService.GetDraw(item.Team, results);
            }

            groupItemsVM.Sort((a, b) => {
                if (a.Points > b.Points)
                {
                    return(1);
                }
                else if (a.Points < b.Points)
                {
                    return(-1);
                }
                else if (a.Goals > b.Goals)
                {
                    return(1);
                }
                else if (a.Goals < b.Goals)
                {
                    return(-1);
                }
                else if (a.GoalDifference > b.GoalDifference)
                {
                    return(1);
                }
                else if (a.GoalDifference < b.GoalDifference)
                {
                    return(-1);
                }
                else
                {
                    return(0);
                }
            });
            groupItemsVM.Reverse();

            for (int i = 0; i < groupItemsVM.Count; i++)
            {
                groupItemsVM[i].Rank = i + 1;
            }

            return(groupItemsVM);
        }