// GET: Matches/Create public async Task <IActionResult> Create(int?id) { if (id == null) { return(NotFound()); } //var fixture = await _context.Fixtures.FindAsync(id); var fixture = await _context.Fixtures .Include(f => f.AwayTeam) .Include(f => f.CaptainApprove) .Include(f => f.CaptainResult) .Include(f => f.Division) .Include(f => f.HomeTeam) .Include(f => f.Season) .Include(f => f.Venue) .FirstOrDefaultAsync(m => m.ID == id); //int nPositions = 4; int nPositions = _context.Divisions.FirstOrDefault(d => d.ID == fixture.DivisionID).PositionNo; if (fixture == null) { return(NotFound()); } FixtureMatchVM fixtureMatch = new FixtureMatchVM(); fixtureMatch.Fixture = fixture; for (int i = 0; i < nPositions; i++) { Match match = new Match(); fixtureMatch.Matches.Add(match); } //.FirstOrDefaultAsync(m => m.ID == fixture.DivisionID); var dQueryHome = from d in _context.Players orderby d.FirstName, d.LastName where d.TeamID == fixture.HomeTeamID || d.ID == 1 select d; var dQueryAway = from d in _context.Players orderby d.FirstName, d.LastName where d.TeamID == fixture.AwayTeamID || d.ID == 1 select d; var homeTeam = _context.Teams.FirstOrDefault(t => t.ID == fixture.HomeTeamID); var awayTeam = _context.Teams.FirstOrDefault(t => t.ID == fixture.AwayTeamID); ViewData["HomeTeam"] = homeTeam; ViewData["AwayTeam"] = awayTeam; ViewData["AwayPlayerID"] = new SelectList(dQueryAway, "ID", "FullName"); ViewData["HomePlayerID"] = new SelectList(dQueryHome, "ID", "FullName"); ViewData["nPositions"] = nPositions; return(View(fixtureMatch)); }
public void UpdateRankings(FixtureMatchVM fixtureMatch) { int nPositions = _context.Divisions.FirstOrDefault(d => d.ID == fixtureMatch.Fixture.DivisionID).PositionNo; Fixture fixture = fixtureMatch.Fixture; TeamRanking teamRankingHome = new TeamRanking(); var teamRankingHomeToUpdate = _context.TeamRankings.FirstOrDefault(d => d.TeamID == fixture.HomeTeamID && d.SeasonID == fixture.SeasonID && d.DivisionID == fixture.DivisionID); TeamRanking teamRankingAway = new TeamRanking(); var teamRankingAwayToUpdate = _context.TeamRankings.FirstOrDefault(d => d.TeamID == fixture.AwayTeamID && d.SeasonID == fixture.SeasonID && d.DivisionID == fixture.DivisionID); if (teamRankingHomeToUpdate == null) { teamRankingHome.TeamID = fixture.HomeTeamID; teamRankingHome.DivisionID = fixture.DivisionID; teamRankingHome.SeasonID = fixture.SeasonID; teamRankingHome.Points = (double)(fixture.HomeTeamScore + fixture.HomeTeamBonus); teamRankingHome.Won = (fixture.HomeTeamScore > fixture.AwayTeamScore) ? 1 : 0; teamRankingHome.Lost = (fixture.HomeTeamScore < fixture.AwayTeamScore) ? 1 : 0; teamRankingHome.Played = 1; _context.Add(teamRankingHome); _context.SaveChanges(); } else { teamRankingHome = teamRankingHomeToUpdate; teamRankingHome.Points += (double)(fixture.HomeTeamScore + fixture.HomeTeamBonus); teamRankingHome.Won += (fixture.HomeTeamScore > fixture.AwayTeamScore) ? 1 : 0; teamRankingHome.Lost += (fixture.HomeTeamScore < fixture.AwayTeamScore) ? 1 : 0; teamRankingHome.Played += 1; _context.Update(teamRankingHome); _context.SaveChanges(); } if (teamRankingAwayToUpdate == null) { teamRankingAway.TeamID = fixture.AwayTeamID; teamRankingAway.DivisionID = fixture.DivisionID; teamRankingAway.SeasonID = fixture.SeasonID; teamRankingAway.Points = (double)(fixture.AwayTeamScore + fixture.AwayTeamBonus); teamRankingAway.Won = (fixture.HomeTeamScore < fixture.AwayTeamScore) ? 1 : 0; teamRankingAway.Lost = (fixture.HomeTeamScore > fixture.AwayTeamScore) ? 1 : 0; teamRankingAway.Played = 1; _context.Add(teamRankingAway); _context.SaveChanges(); } else { teamRankingAway = teamRankingAwayToUpdate; teamRankingAway.Points += (double)(fixture.AwayTeamScore + fixture.AwayTeamBonus); teamRankingAway.Won += (fixture.HomeTeamScore < fixture.AwayTeamScore) ? 1 : 0; teamRankingAway.Lost += (fixture.HomeTeamScore > fixture.AwayTeamScore) ? 1 : 0; teamRankingAway.Played += 1; _context.Update(teamRankingAway); _context.SaveChanges(); } //Loop trough the matches to update the rankings foreach (Match match in fixtureMatch.Matches) { if (match.HomePlayerID != 1) { //Add statistics for home player PlayerRanking playerRankingHome = new PlayerRanking(); var playerRankingHomeToUpdate = _context.PlayerRankings.FirstOrDefault(d => d.PlayerID == match.HomePlayerID && d.SeasonID == fixture.SeasonID && d.DivisionID == fixture.DivisionID); if (playerRankingHomeToUpdate == null) { playerRankingHome.TotalPositions = (int)match.PositionID; playerRankingHome.PlayerID = match.HomePlayerID; playerRankingHome.SeasonID = fixture.SeasonID; playerRankingHome.DivisionID = fixture.DivisionID; playerRankingHome.Played = 1; if (match.HomePlayerScore > match.AwayPlayerScore) { playerRankingHome.WonMatches = 1; playerRankingHome.LostMatches = 0; } else { playerRankingHome.WonMatches = 0; playerRankingHome.LostMatches = 1; } playerRankingHome.WonGames = (short)match.HomePlayerScore; playerRankingHome.LostGames = (short)match.AwayPlayerScore; playerRankingHome.Points = RankPlayer.CalcPoints(match.Position.Name, (short)match.HomePlayerScore, (short)match.AwayPlayerScore, ResultFor.Home); playerRankingHome.Average = playerRankingHome.Points / playerRankingHome.Played; _context.Add(playerRankingHome); } else { playerRankingHome = playerRankingHomeToUpdate; playerRankingHome.Played += 1; playerRankingHome.TotalPositions += (int)match.PositionID; if (match.HomePlayerScore > match.AwayPlayerScore) { playerRankingHome.WonMatches += 1; } else { playerRankingHome.LostMatches += 1; } playerRankingHome.WonGames += (short)match.HomePlayerScore; playerRankingHome.LostGames += (short)match.AwayPlayerScore; playerRankingHome.Points += RankPlayer.CalcPoints(match.Position.Name, (short)match.HomePlayerScore, (short)match.AwayPlayerScore, ResultFor.Home); playerRankingHome.Average = playerRankingHome.Points / playerRankingHome.Played; _context.Update(playerRankingHome); } } if (match.AwayPlayerID != 1) { //Add statistics for away player PlayerRanking playerRankingAway = new PlayerRanking(); var playerRankingAwayToUpdate = _context.PlayerRankings.FirstOrDefault(d => d.PlayerID == match.AwayPlayerID && d.SeasonID == fixture.SeasonID && d.DivisionID == fixture.DivisionID); if (playerRankingAwayToUpdate == null) { playerRankingAway.PlayerID = match.AwayPlayerID; playerRankingAway.SeasonID = fixture.SeasonID; playerRankingAway.DivisionID = fixture.DivisionID; playerRankingAway.Played = 1; playerRankingAway.TotalPositions = (int)match.PositionID; if (match.HomePlayerScore < match.AwayPlayerScore) { playerRankingAway.WonMatches = 1; playerRankingAway.LostMatches = 0; } else { playerRankingAway.WonMatches = 0; playerRankingAway.LostMatches = 1; } playerRankingAway.WonGames = (short)match.AwayPlayerScore; playerRankingAway.LostGames = (short)match.HomePlayerScore; playerRankingAway.Points = RankPlayer.CalcPoints(match.Position.Name, (short)match.HomePlayerScore, (short)match.AwayPlayerScore, ResultFor.Away); playerRankingAway.Average = playerRankingAway.Points / playerRankingAway.Played; _context.Add(playerRankingAway); } else { playerRankingAway = playerRankingAwayToUpdate; playerRankingAway.Played += 1; playerRankingAway.TotalPositions += (int)match.PositionID; if (match.HomePlayerScore < match.AwayPlayerScore) { playerRankingAway.WonMatches += 1; } else { playerRankingAway.LostMatches += 1; } playerRankingAway.WonGames += (short)match.AwayPlayerScore; playerRankingAway.LostGames += (short)match.HomePlayerScore; playerRankingAway.Points += RankPlayer.CalcPoints(match.Position.Name, (short)match.HomePlayerScore, (short)match.AwayPlayerScore, ResultFor.Away); playerRankingAway.Average = playerRankingAway.Points / playerRankingAway.Played; _context.Update(playerRankingAway); } } } _context.SaveChanges(); }
public void checkViolation(FixtureMatchVM fixtureMatch) { var fixtureDate = fixtureMatch.Fixture.Date; foreach (Match match in fixtureMatch.Matches) { int homePlayerID = match.HomePlayerID; int awayPlayerID = match.AwayPlayerID; int?positionID = match.PositionID; //var beforeHomeFixture = (from f in _context.Fixtures // join m in _context.Matches on f.ID equals m.FixtureID // where f.Date < fixtureDate && (f.HomeTeamID == homeTeamID || f.AwayTeamID == homeTeamID) && m.HomePlayerID // orderby f.Date descending // select f).FirstOrDefault(); var beforeHomeMatches = (from m in _context.Matches join f in _context.Fixtures on m.FixtureID equals f.ID where f.Date < fixtureDate && (m.HomePlayerID == homePlayerID || m.AwayPlayerID == homePlayerID) orderby f.Date descending select m).FirstOrDefault(); var beforeAwayMatches = (from m in _context.Matches join f in _context.Fixtures on m.FixtureID equals f.ID where f.Date < fixtureDate && (m.HomePlayerID == awayPlayerID || m.AwayPlayerID == awayPlayerID) orderby f.Date descending select m).FirstOrDefault(); /* * SELECT * from SQUASH.Matches * JOIN SQUASH.Fixtures on SQUASH.Fixtures.ID = SQUASH.Matches.FixtureID * WHERE SQUASH.Fixtures.Date < '2018-10-20' and (HomePlayerID = 8 OR AwayPlayerID = 8) * ORDER BY SQUASH.Fixtures.Date desc */ if (positionID == 1) { if (beforeHomeMatches != null) { if (beforeHomeMatches.PositionID == 3 || beforeHomeMatches.PositionID == 4 || beforeHomeMatches.PositionID == 5) { match.HomePlayerViolation = true; } else { match.HomePlayerViolation = false; } } if (beforeAwayMatches != null) { if (beforeAwayMatches.PositionID == 3 || beforeAwayMatches.PositionID == 4 || beforeAwayMatches.PositionID == 5) { match.AwayPlayerViolation = true; } else { match.AwayPlayerViolation = false; } } } else if (positionID == 2) { if (beforeHomeMatches != null) { if (beforeHomeMatches.PositionID == 4 || beforeHomeMatches.PositionID == 5) { match.HomePlayerViolation = true; } else { match.HomePlayerViolation = false; } } else { match.HomePlayerViolation = false; } if (beforeAwayMatches != null) { if (beforeAwayMatches.PositionID == 4 || beforeAwayMatches.PositionID == 5) { match.AwayPlayerViolation = true; } else { match.AwayPlayerViolation = false; } } else { match.AwayPlayerViolation = false; } } else if (positionID == 3) { if (beforeHomeMatches != null) { if (beforeHomeMatches.PositionID == 1 || beforeHomeMatches.PositionID == 5) { match.HomePlayerViolation = true; } else { match.HomePlayerViolation = false; } } else { match.HomePlayerViolation = false; } if (beforeAwayMatches != null) { if (beforeAwayMatches.PositionID == 1 || beforeAwayMatches.PositionID == 5) { match.AwayPlayerViolation = true; } else { match.AwayPlayerViolation = false; } } else { match.AwayPlayerViolation = false; } } else if (positionID == 4) { if (beforeHomeMatches != null) { if (beforeHomeMatches.PositionID == 1 || beforeHomeMatches.PositionID == 2) { match.HomePlayerViolation = true; } else { match.HomePlayerViolation = false; } } else { match.HomePlayerViolation = false; } if (beforeAwayMatches != null) { if (beforeAwayMatches.PositionID == 1 || beforeAwayMatches.PositionID == 2) { match.AwayPlayerViolation = true; } else { match.AwayPlayerViolation = false; } } else { match.AwayPlayerViolation = false; } } else if (positionID == 5) { if (beforeHomeMatches != null) { if (beforeHomeMatches.PositionID == 1 || beforeHomeMatches.PositionID == 2 || beforeHomeMatches.PositionID == 3) { match.HomePlayerViolation = true; } else { match.HomePlayerViolation = false; } } else { match.HomePlayerViolation = false; } if (beforeAwayMatches != null) { if (beforeAwayMatches.PositionID == 1 || beforeAwayMatches.PositionID == 2 || beforeAwayMatches.PositionID == 3) { match.AwayPlayerViolation = true; } else { match.AwayPlayerViolation = false; } } else { match.AwayPlayerViolation = false; } } _context.Update(match); _context.SaveChanges(); } }
public async Task <IActionResult> ApproveResult(int id, [Bind("ID,SeasonID,DivisionID,HomeTeamID,AwayTeamID,Date,Time,VenueID,HomeTeamScore,AwayTeamScore,HomeTeamBonus,AwayTeamBonus,Approved,CaptainResultID,CaptainApproveID")] Fixture fixture) { if (id != fixture.ID) { return(NotFound()); } if (fixture.Approved == false) { return(RedirectToAction(nameof(Index))); } if (ModelState.IsValid) { try { if (User.IsInRole("Captain")) { fixture.CaptainApproveID = _context.Players.FirstOrDefault(p => p.Email == User.Identity.Name).ID; } _context.Update(fixture); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!FixtureExists(fixture.ID)) { return(NotFound()); } else { throw; } } FixtureMatchVM fixtureMatch = new FixtureMatchVM(); fixtureMatch.Fixture = _context.Fixtures.Find(id); var matchesFromFixture = from m in _context.Matches .Where(m => m.FixtureID == id) .Include(m => m.Fixture) .Include(m => m.Position) .Include(m => m.HomePlayer) .Include(m => m.AwayPlayer) select m; fixtureMatch.Matches = matchesFromFixture.ToList(); UpdateRankings(fixtureMatch); checkViolation(fixtureMatch); return(RedirectToAction(nameof(Index))); } ViewData["AwayTeamID"] = new SelectList(_context.Teams, "ID", "Name", fixture.AwayTeamID); ViewData["CaptainApproveID"] = new SelectList(_context.Players, "ID", "Email", fixture.CaptainApproveID); ViewData["CaptainResultID"] = new SelectList(_context.Players, "ID", "Email", fixture.CaptainResultID); ViewData["DivisionID"] = new SelectList(_context.Divisions, "ID", "Name", fixture.DivisionID); ViewData["HomeTeamID"] = new SelectList(_context.Teams, "ID", "Name", fixture.HomeTeamID); ViewData["SeasonID"] = new SelectList(_context.Seasons, "ID", "Name", fixture.SeasonID); ViewData["VenueID"] = new SelectList(_context.Venues, "ID", "Name", fixture.VenueID); return(View(fixture)); }
public async Task <IActionResult> Edit(FixtureMatchVM fixtureMatch, string Bonus) { //get the number of the positions for the division; int nPositions = _context.Divisions.FirstOrDefault(d => d.ID == fixtureMatch.Fixture.DivisionID).PositionNo; if (Bonus == "Home") { fixtureMatch.Fixture.HomeTeamBonus = 1d; fixtureMatch.Fixture.AwayTeamBonus = 0d; } else if (Bonus == "Away") { fixtureMatch.Fixture.HomeTeamBonus = 0d; fixtureMatch.Fixture.AwayTeamBonus = 1d; } else if (Bonus == "Tie") { fixtureMatch.Fixture.HomeTeamBonus = 0.5d; fixtureMatch.Fixture.AwayTeamBonus = 0.5d; } Fixture fixture = fixtureMatch.Fixture; try { if (ModelState.IsValid) { //Get the fixture from model FixtureMatch //Fixture fixture = fixtureMatch.Fixture; fixture.HomeTeamScore = 0; fixture.AwayTeamScore = 0; for (int n = 0; n < nPositions; n++) { if (fixtureMatch.Matches[n].HomePlayerScore > fixtureMatch.Matches[n].AwayPlayerScore) { fixture.HomeTeamScore += 1; } else if (fixtureMatch.Matches[n].HomePlayerScore < fixtureMatch.Matches[n].AwayPlayerScore) { fixture.AwayTeamScore += 1; } else { ModelState.AddModelError("Score error", "No results can be blank"); return(View(fixtureMatch)); } //Save the fixture in the DB if (User.IsInRole("Captain")) { fixture.CaptainResultID = _context.Players.FirstOrDefault(p => p.Email == User.Identity.Name).ID; } _context.Update(fixture); } //Loop trough the matches to get the matches results and add in the DB for (int i = 0; i < nPositions; i++) { //Capture the result of the match and save in the database Match match = fixtureMatch.Matches[i]; match.PositionID = _context.Positions.FirstOrDefault(d => d.Name.Contains((i + 1).ToString())).ID; _context.Update(match); //Add in the DB the PlayerPosition for home player PlayerPosition playerPositionHome = new PlayerPosition { PlayerID = match.HomePlayerID, MatchID = match.ID, PositionID = _context.Positions.FirstOrDefault(d => d.Name.Contains((i + 1).ToString())).ID, }; _context.Update(playerPositionHome); //Add in the DB the PlayerPosition for away player PlayerPosition playerPositionAway = new PlayerPosition { PlayerID = match.AwayPlayerID, MatchID = match.ID, PositionID = _context.Positions.FirstOrDefault(d => d.Name.Contains((i + 1).ToString())).ID, }; _context.Update(playerPositionAway); } await _context.SaveChangesAsync(); return(RedirectToAction("Index", "Fixtures")); } } catch (DbUpdateException err) { if (err.InnerException.Message.Contains("IX_Matches_FixtureID_HomePlayerID_AwayPlayerID")) { ModelState.AddModelError("Player error", "One player cannot play two matches in the same fixture"); } else { ModelState.AddModelError("", "Unable to save result. Report this error to the team"); } } catch (Exception) { ModelState.AddModelError("", "Unable to save result. Report this error to the team"); } finally { var dQueryHome = from d in _context.Players orderby d.FirstName, d.LastName where d.TeamID == fixtureMatch.Fixture.HomeTeamID || d.ID == 1 select d; var dQueryAway = from d in _context.Players orderby d.FirstName, d.LastName where d.TeamID == fixtureMatch.Fixture.AwayTeamID || d.ID == 1 select d; //ViewData["AwayPlayerID"] = new SelectList(dQueryAway, "ID", "FullName"); //ViewData["HomePlayerID"] = new SelectList(dQueryHome, "ID", "FullName"); //ViewData["nPositions"] = nPositions; int pos1ID = _context.Positions.FirstOrDefault(d => d.Name.Contains("Position 1")).ID; int pos2ID = _context.Positions.FirstOrDefault(d => d.Name.Contains("Position 2")).ID; int pos3ID = _context.Positions.FirstOrDefault(d => d.Name.Contains("Position 3")).ID; int pos4ID = _context.Positions.FirstOrDefault(d => d.Name.Contains("Position 4")).ID; int pos5ID = 0; if (nPositions == 5) { pos5ID = _context.Positions.FirstOrDefault(d => d.Name.Contains("Position 5")).ID; } int hpPos1 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos1ID).HomePlayerID; int apPos1 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos1ID).AwayPlayerID; int hpPos2 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos2ID).HomePlayerID; int apPos2 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos2ID).AwayPlayerID; int hpPos3 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos3ID).HomePlayerID; int apPos3 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos3ID).AwayPlayerID; int hpPos4 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos4ID).HomePlayerID; int apPos4 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos4ID).AwayPlayerID; int hpPos5 = 0; int apPos5 = 0; if (nPositions == 5) { hpPos5 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos5ID).HomePlayerID; apPos5 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos5ID).AwayPlayerID; } ViewData["HomePlayerIDPos1"] = new SelectList(dQueryHome, "ID", "FullName", hpPos1); ViewData["HomePlayerIDPos2"] = new SelectList(dQueryHome, "ID", "FullName", hpPos2); ViewData["HomePlayerIDPos3"] = new SelectList(dQueryHome, "ID", "FullName", hpPos3); ViewData["HomePlayerIDPos4"] = new SelectList(dQueryHome, "ID", "FullName", hpPos4); if (nPositions == 5) { ViewData["HomePlayerIDPos5"] = new SelectList(dQueryHome, "ID", "FullName", hpPos5); } ViewData["AwayPlayerIDPos1"] = new SelectList(dQueryAway, "ID", "FullName", apPos1); ViewData["AwayPlayerIDPos2"] = new SelectList(dQueryAway, "ID", "FullName", apPos2); ViewData["AwayPlayerIDPos3"] = new SelectList(dQueryAway, "ID", "FullName", apPos3); ViewData["AwayPlayerIDPos4"] = new SelectList(dQueryAway, "ID", "FullName", apPos4); if (nPositions == 5) { ViewData["AwayPlayerIDPos5"] = new SelectList(dQueryAway, "ID", "FullName", apPos5); } ViewData["nPositions"] = nPositions; } return(View(fixtureMatch)); }
// GET: Matches/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } //var fixture = await _context.Fixtures.FindAsync(id); var fixture = await _context.Fixtures .Include(f => f.AwayTeam) .Include(f => f.CaptainApprove) .Include(f => f.CaptainResult) .Include(f => f.Division) .Include(f => f.HomeTeam) .Include(f => f.Season) .Include(f => f.Venue) .FirstOrDefaultAsync(m => m.ID == id); var matches = _context.Matches .Include(m => m.Fixture) .Include(m => m.HomePlayer) .Include(m => m.AwayPlayer) .Include(m => m.Position) .Where(m => m.FixtureID == id) .OrderBy(m => m.Position.Name); //int nPositions = 4; int nPositions = _context.Divisions.FirstOrDefault(d => d.ID == fixture.DivisionID).PositionNo; if (fixture == null) { return(NotFound()); } FixtureMatchVM fixtureMatch = new FixtureMatchVM(); fixtureMatch.Fixture = fixture; foreach (Match match in matches) { fixtureMatch.Matches.Add(match); } //.FirstOrDefaultAsync(m => m.ID == fixture.DivisionID); var dQueryHome = from d in _context.Players orderby d.FirstName, d.LastName where d.TeamID == fixture.HomeTeamID || d.ID == 1 select d; var dQueryAway = from d in _context.Players orderby d.FirstName, d.LastName where d.TeamID == fixture.AwayTeamID || d.ID == 1 select d; int pos1ID = _context.Positions.FirstOrDefault(d => d.Name.Contains("Position 1")).ID; int pos2ID = _context.Positions.FirstOrDefault(d => d.Name.Contains("Position 2")).ID; int pos3ID = _context.Positions.FirstOrDefault(d => d.Name.Contains("Position 3")).ID; int pos4ID = _context.Positions.FirstOrDefault(d => d.Name.Contains("Position 4")).ID; int pos5ID = 0; if (nPositions == 5) { pos5ID = _context.Positions.FirstOrDefault(d => d.Name.Contains("Position 5")).ID; } int hpPos1 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos1ID).HomePlayerID; int apPos1 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos1ID).AwayPlayerID; int hpPos2 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos2ID).HomePlayerID; int apPos2 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos2ID).AwayPlayerID; int hpPos3 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos3ID).HomePlayerID; int apPos3 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos3ID).AwayPlayerID; int hpPos4 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos4ID).HomePlayerID; int apPos4 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos4ID).AwayPlayerID; int hpPos5 = 0; int apPos5 = 0; if (nPositions == 5) { hpPos5 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos5ID).HomePlayerID; apPos5 = _context.Matches.FirstOrDefault(d => d.FixtureID == fixture.ID && d.PositionID == pos5ID).AwayPlayerID; } ViewData["HomePlayerIDPos1"] = new SelectList(dQueryHome, "ID", "FullName", hpPos1); ViewData["HomePlayerIDPos2"] = new SelectList(dQueryHome, "ID", "FullName", hpPos2); ViewData["HomePlayerIDPos3"] = new SelectList(dQueryHome, "ID", "FullName", hpPos3); ViewData["HomePlayerIDPos4"] = new SelectList(dQueryHome, "ID", "FullName", hpPos4); if (nPositions == 5) { ViewData["HomePlayerIDPos5"] = new SelectList(dQueryHome, "ID", "FullName", hpPos5); } ViewData["AwayPlayerIDPos1"] = new SelectList(dQueryAway, "ID", "FullName", apPos1); ViewData["AwayPlayerIDPos2"] = new SelectList(dQueryAway, "ID", "FullName", apPos2); ViewData["AwayPlayerIDPos3"] = new SelectList(dQueryAway, "ID", "FullName", apPos3); ViewData["AwayPlayerIDPos4"] = new SelectList(dQueryAway, "ID", "FullName", apPos4); if (nPositions == 5) { ViewData["AwayPlayerIDPos5"] = new SelectList(dQueryAway, "ID", "FullName", apPos5); } ViewData["nPositions"] = nPositions; return(View(fixtureMatch)); }