internal List <PlayerStandingsResultsDto> GetStandings(string divisionName, string seasonName, string leagueName) { // Include certain properties on Match. var loadOptions = new DataLoadOptions(); loadOptions.LoadWith <Match>(x => x.MatchSets); loadOptions.LoadWith <Match>(x => x.Division); loadOptions.LoadWith <Match>(x => x.Player); loadOptions.LoadWith <Match>(x => x.Player1); this.LoadOptions = loadOptions; var query = from match in Matches where match.Division.Name == divisionName && match.Division.Season.Name == seasonName && match.Division.Season.League.Name == leagueName orderby match.Id select match; var playerIdToStandingsMapping = new Dictionary <int, PlayerStandingsResultsDto>(); var standings = new PlayerStandingsResultsDto(); int previousMatchId = 0; foreach (var match in query) { if (match.Id == previousMatchId) { continue; } previousMatchId = match.Id; UpdateHomeResults(match, playerIdToStandingsMapping); UpdateAwayResults(match, playerIdToStandingsMapping); } //return playerIdToStandingsMapping.Values.ToList() // .OrderByDescending(x => x.MatchesWon).ThenByDescending(x => x.SetsWon).ThenByDescending(x => x.GamesWon) // .ThenBy(x => x.SetsLost).ThenBy(x => x.GamesLost).ToList(); return(playerIdToStandingsMapping.Values.ToList() .OrderByDescending(x => x.MatchesWon - x.MatchesLost) .ThenByDescending(x => x.SetsWon - x.SetsLost) .ThenByDescending(x => x.GamesWon - x.GamesLost) .ToList()); }
private static PlayerStandingsResultsDto GetResultsDto(Dictionary <int, PlayerStandingsResultsDto> standingsDictionary, Player player) { PlayerStandingsResultsDto resultsDto; if (standingsDictionary.ContainsKey(player.Id)) { resultsDto = standingsDictionary[player.Id]; } else { resultsDto = new PlayerStandingsResultsDto(); resultsDto.PlayerId = player.Id; resultsDto.PlayerName = player.FirstName + " " + player.LastName; standingsDictionary.Add(player.Id, resultsDto); } return(resultsDto); }
private static void UpdateAwayResults(Match match, Dictionary <int, PlayerStandingsResultsDto> standingsDictionary) { PlayerStandingsResultsDto resultsAway = GetResultsDto(standingsDictionary, match.Player1); var homeSetsWon = match.MatchSets.Count(x => x.HomeGamesWon > x.AwayGamesWon); var awaySetsWon = match.MatchSets.Count(x => x.AwayGamesWon > x.HomeGamesWon); if (homeSetsWon > awaySetsWon) { resultsAway.MatchesLost += 1; } else if (awaySetsWon > homeSetsWon) // they could be the same (0-0) if the match hasn't been played yet { resultsAway.MatchesWon += 1; } resultsAway.GamesWon += match.MatchSets.Sum(x => x.AwayGamesWon); resultsAway.GamesLost += match.MatchSets.Sum(x => x.HomeGamesWon); resultsAway.SetsWon += awaySetsWon; resultsAway.SetsLost += homeSetsWon; }