public IEnumerable<ParticipantView> GetAllDeelnemers()
        {
            HetEKSpelEntities db = new HetEKSpelEntities(connectionString);

            var query = db.Participants;

            var participantsList = new List<ParticipantView>();

            foreach (var participant in query)
            {
                var newParticipant = new ParticipantView
                {
                    ParticipantId = participant.Id,
                    Location = participant.Location,
                    Name = participant.Name,
                    Gender = participant.Gender
                };

                participantsList.Add(newParticipant);
            }
            return participantsList.OrderBy(p=>p.ParticipantId);
        }
        public SecondroundPredictionsParticipantOverview GetParticipantsForTeamPerPredictionType(int teamId, int predictionType)
        {
            var participants = db.Participants.ToList();
            var predictions = db.Predictions.ToList();
            var teamName = db.Teams.Single(t => t.Id == teamId).Team1;
            var latestTableId = Standen.GetLatestTableId();

            var table = db.Tables.Where(t => t.TableId == latestTableId).ToList();

            var participantView = new List<ParticipantView>();
            var predictionList = predictions.Where(p => p.PredictionType_Id == predictionType && p.Team_Id == teamId);

                foreach (var item in predictionList)
                {
                    var participant = participants.Single(p => p.Id == item.Participant_Id);
                    var line = new ParticipantView
                                   {

                                       ParticipantId = participant.Id,
                                       Name = participant.Name,
                                       Position = table.Single(t=>t.ParticipantId == participant.Id).Position,
                                       Points = table.Single(t=>t.ParticipantId == participant.Id).TotalPoints.ToString()

                                   };
                participantView.Add(line);
                }

            var orderedparticipantView = participantView.OrderBy(p => p.Position).ToList();
            var secondroundPredictionsParticipantOverview = new SecondroundPredictionsParticipantOverview
                           {
                               Participant = orderedparticipantView,
                               Round = "",
                               Team = teamName
                           };

            return secondroundPredictionsParticipantOverview;
        }
        public RightPrediction GetRightPredictions()
        {
            var rightGamepredictionList = new List<RightGamePrediction>();
            var rightSecondRoundPredictionList = new List<RightSecondRoundPrediction>();

            var teams = db.Teams.ToList();
            var playedGames = db.Games.Where(g => g.Played == true).ToList();
            var gameGames = playedGames.Where(g => g.PredictionType_Id == PredictionType.Game).OrderBy(g=>g.sortId);
            var secondRoundGames =
                playedGames.Where(
                    g => g.PredictionType_Id != PredictionType.Game && g.PredictionType_Id != PredictionType.Table).ToList();

            var predictions = db.Predictions.ToList();
            var participants = db.Participants.ToList();

            #region

            foreach (var game in gameGames)
            {
                var points = Standen.GetMaxScoreForPredictionType(game.PredictionType_Id);

                var rightGamePredictions =
                    predictions.Where(
                        p =>
                        p.Game_Id == game.Id &&
                        p.Points ==
                        (points)).ToList();

                var participantList = new List<ParticipantView>();

                foreach (var rightGamePrediction in rightGamePredictions)
                {
                    var participant = participants.SingleOrDefault(
                        p => p.Id == rightGamePrediction.Participant_Id);

                    var participantLine = new ParticipantView
                                              {
                                                  Name = participant.Name,
                                                  ParticipantId = participant.Id
                                              };

                    participantList.Add(participantLine);

                }

                var orderedParticipantList = participantList.OrderBy(p => p.Name).ToList();

                var homeTeam = teams.SingleOrDefault(t => t.Id == game.HomeTeam_Id).Team1;
                var awayTeam = teams.SingleOrDefault(t => t.Id == game.AwayTeam_Id).Team1;

                var gameView = new GameView
                                   {
                                       AwayScore = game.AwayScore.ToString(),
                                       HomeScore = game.HomeScore.ToString(),
                                       HomeTeam = homeTeam,
                                       AwayTeam = awayTeam,
                                       GameId = game.Id,
                                       Played = (bool) game.Played,
                                       Date = game.Date.ToString(),
                                       SortId = (int) game.sortId

                                   };

                var firstRoundPrediction = new RightGamePrediction
                                               {
                                                   GameView = gameView,
                                                   Participants = orderedParticipantList
                                               };
                rightGamepredictionList.Add(firstRoundPrediction);
            }

            #endregion

            foreach (var game in secondRoundGames)
            {
                var points = Standen.GetMaxScoreForPredictionType(game.PredictionType_Id);

                var rightSecondRoundPredictions = predictions.Where(
                    p =>
                    p.Team_Id == game.Team_Id &&
                    p.PredictionType_Id == game.PredictionType_Id &&
                    p.Points ==
                    (points)).ToList();

                var participantList = new List<ParticipantView>();

                foreach (var rightSecondRoundPrediction in rightSecondRoundPredictions)
                {
                    var participant = participants.SingleOrDefault(
                        p => p.Id == rightSecondRoundPrediction.Participant_Id);

                    var participantLine = new ParticipantView
                                              {
                                                  Name = participant.Name,
                                                  ParticipantId = participant.Id
                                              };

                    participantList.Add(participantLine);
                }
                var orderedParticipantList = participantList.OrderBy(p => p.Name).ToList();

                var homeTeam = teams.SingleOrDefault(t => t.Id == game.Team_Id).Team1;
                var roundName = "";
                switch (game.PredictionType_Id)
                {
                    case PredictionType.QuarterFinals:
                        {
                            roundName = "in de kwartfinale";
                            break;
                        }

                    case PredictionType.SemiFinal:
                        {
                            roundName = "in de halve finale";
                            break;
                        }
                    case PredictionType.Final:
                        {
                            roundName = "in de finale";
                            break;
                        }
                    case PredictionType.Winner:
                        {
                            roundName = "als winnaar";
                            break;
                        }
                }

                var gameView = new GameView
                                   {
                                       HomeTeam = homeTeam,
                                     RoundName = roundName,

                                   };

                var secondRoundPrediction = new RightSecondRoundPrediction()
                                                {
                                                    GameView = gameView,
                                                    Participants = orderedParticipantList
                                                };
                rightSecondRoundPredictionList.Add(secondRoundPrediction);
            }

            var orderedFirstRoundPredicionts = rightGamepredictionList.OrderByDescending(r => r.GameView.SortId).ToList();
            var rightpredictions = new RightPrediction
                                       {
                                           FirstRoundRightPredictions = orderedFirstRoundPredicionts,
                                           SecondRoundPredictions = rightSecondRoundPredictionList
                                       };

            return rightpredictions;
        }
        public RightPrediction GetRightPredictions(int id)
        {
            var game = db.Games.SingleOrDefault(g => g.Id == id);
            var points = Standen.GetMaxScoreForPredictionType(game.PredictionType_Id);

            var rightPredictions =
                db.Predictions.Where(
                    p =>
                    p.Game_Id == game.Id &&
                    p.Points ==
                    (points));

            var participants = db.Participants.ToList();

            var participantList = new List<ParticipantView>();

            foreach (var rightPrediction in rightPredictions)
            {
                var participant = participants.SingleOrDefault(
                    p => p.Id == rightPrediction.Participant_Id);

                var participantLine = new ParticipantView
                                          {
                                              Name = participant.Name,
                                              ParticipantId = participant.Id
                                          };

                participantList.Add(participantLine);
            }

            var homeTeam = db.Teams.SingleOrDefault(t => t.Id == game.HomeTeam_Id).Team1;
            var awayTeam = db.Teams.SingleOrDefault(t => t.Id == game.AwayTeam_Id).Team1;

            var gameView = new GameView
                               {
                                   AwayScore =  game.AwayScore.ToString(),
                                   HomeScore =  game.HomeScore.ToString(),
                                   HomeTeam = homeTeam,
                                   AwayTeam = awayTeam,
                                   GameId = game.Id,
                                   Played = (bool) game.Played,
                                   Date =  game.Date.ToString()

                               };

            var rightPrediction1 = new RightPrediction
                                       {
                                           FirstRoundRightPredictions = new List<RightGamePrediction>
                                                                            {new RightGamePrediction
                                                                                      {

                                                                                          GameView = gameView,
                                                                                          Participants = participantList
                                                                                      }}
                                       };

            return rightPrediction1;
        }
        public IEnumerable<ParticipantView> GetDeelnemerById(int id)
        {
            HetEKSpelEntities db = new HetEKSpelEntities(connectionString);

            //if (Id < 0 || Id > db.Participants.Count())
            //{
            //    throw new HttpResponseException(System.Net.HttpStatusCode.NotFound);
            //}

            //            var participantId = db.Tables.Single(t => t.Id == id).ParticipantId;

            var participant = db.Participants.SingleOrDefault(p => p.Id == id);
            var allTables = db.Tables;
            var latestTableId =  allTables.Max(t => t.TableId);

            var latestTable = allTables.Where(t => t.TableId == latestTableId);
            var maxTableLineId = latestTable.Max(t => t.Id);
            var minTablelineId = latestTable.Min(t => t.Id);
            var tables = db.Tables.Where(t => t.ParticipantId == id).ToList();
            var tableLine = tables.LastOrDefault();

            var nextParticipantId = "";
            var previousParticipantId = "";

            if (tableLine.Id != minTablelineId)
            {
               previousParticipantId = latestTable.Single(t => t.Id == tableLine.Id - 1).ParticipantId.ToString();

            }

            if (tableLine.Id != maxTableLineId)
            {
                nextParticipantId = latestTable.Single(t => t.Id == tableLine.Id + 1).ParticipantId.ToString();

            }

            var deelnemersList = new List<ParticipantView>();

            var deelnemer = new ParticipantView
                                {
                                    ParticipantId = participant.Id,
                                    Location = participant.Location,
                                    Name = participant.Name,
                                    Gender = participant.Gender,
                                    Points = tableLine.TotalPoints.ToString(),
                                    Position = tableLine.Position,
                                    TablelineId = tableLine.Id.ToString(),
                                    MaxTablelineId = maxTableLineId.ToString(),
                                    MinTablelineId = minTablelineId.ToString(),
                                    NextParticipantId = nextParticipantId,
                                    PreviousParticipantId = previousParticipantId
                                };

            deelnemersList.Add(deelnemer);

            return deelnemersList;
        }