public TeamStandingsDataDTO MapToTeamStandingsDTO(TeamStandingsEntity source, TeamStandingsDataDTO target = null) { if (source == null) { return(null); } if (target == null) { target = new TeamStandingsDataDTO(); } MapToStandingsDataDTO(source, target); var teamStandingsRows = source.StandingsRows.OfType <TeamStandingsRowEntity>(); target.StandingsRows = teamStandingsRows.Select(x => MapToTeamStandingsRowDTO(x)).ToArray(); return(target); }
public TeamStandingsEntity GetSeasonTeamStandings(SessionBaseEntity currentSession, LeagueDbContext dbContext, int maxRacesCount = -1) { if (currentSession == null) { return(null); } if (maxRacesCount == -1) { maxRacesCount = Sessions.Count() - maxRacesCount; } var allScoredResults = Scorings?.SelectMany(x => x.ScoredResults).OfType <ScoredTeamResultEntity>().ToList(); var previousScoredResults = allScoredResults.Where(x => x.Result.Session.Date < currentSession.Date).ToList(); foreach (var scoredTeamResult in previousScoredResults) { dbContext.Entry(scoredTeamResult).Collection(x => x.TeamResults).Query() .Include(x => x.Team) .Include(x => x.ScoredResultRows.Select(y => y.ScoredResult)).Load(); } var currentResult = currentSession.SessionResult; var currentScoredResult = allScoredResults.SingleOrDefault(x => x.Result.Session == currentSession); dbContext.Entry(currentScoredResult).Collection(x => x.TeamResults).Query() .Include(x => x.Team) .Include(x => x.ScoredResultRows.Select(y => y.ScoredResult)).Load(); TeamStandingsEntity teamStandings = new TeamStandingsEntity() { ScoringTable = this, SessionId = currentSession.SessionId }; var previousScoredRows = previousScoredResults.SelectMany(x => x.TeamResults).ToList(); IEnumerable <TeamStandingsRowEntity> previousStandingsRows; IEnumerable <TeamStandingsRowEntity> currentStandingsRows; if (DropRacesOption == DropRacesOption.PerDriverResults) { var allScoredDriverResults = Scorings?.SelectMany(x => x.GetResultsFromSource()).ToList(); var previousScoredDriverResults = allScoredDriverResults.Where(x => x.Result.Session.Date < currentSession.Date).ToList(); var currentDriverResult = currentSession.SessionResult; var currentScoredDriverResult = allScoredDriverResults.SingleOrDefault(x => x.Result.Session == currentSession); var previousScoredDriverRows = previousScoredDriverResults.SelectMany(x => x.FinalResults); previousStandingsRows = previousScoredRows.AggregateByTeam(maxRacesCount, true, DropRacesOption, ResultsPerRaceCount, previousScoredDriverRows) .OrderBy(x => - x.TotalPoints); allScoredDriverResults = previousScoredDriverResults.ToList(); allScoredDriverResults.Add(currentScoredDriverResult); allScoredResults = previousScoredResults.ToList(); allScoredResults.Add(currentScoredResult); var allScoredDriverRows = allScoredDriverResults.SelectMany(x => x.FinalResults); currentStandingsRows = allScoredResults.SelectMany(x => x.TeamResults) .AggregateByTeam(maxRacesCount, true, DropRacesOption, ResultsPerRaceCount, allScoredDriverRows) .OrderBy(x => - x.TotalPoints) .ThenBy(x => x.PenaltyPoints) .ThenBy(x => x.Wins); } else { previousStandingsRows = previousScoredRows.AggregateByTeam(maxRacesCount, true, DropRacesOption, ResultsPerRaceCount).OrderBy(x => - x.TotalPoints); allScoredResults = previousScoredResults.ToList(); allScoredResults.Add(currentScoredResult); currentStandingsRows = allScoredResults.SelectMany(x => x.TeamResults).AggregateByTeam(maxRacesCount, true, DropRacesOption, ResultsPerRaceCount).OrderBy(x => - x.TotalPoints); } previousStandingsRows.Select((value, index) => new { index, value }).ToList().ForEach(x => x.value.Position = x.index + 1); currentStandingsRows.Select((value, index) => new { index, value }).ToList().ForEach(x => x.value.Position = x.index + 1); teamStandings.StandingsRows = currentStandingsRows .Diff(previousStandingsRows) .OrderBy(x => - x.TotalPoints) .ThenBy(x => x.PenaltyPoints) .ThenBy(x => - x.Wins) .ToList(); //teamStandings.StandingsRows = currentStandingsRows.OrderBy(x => -x.TotalPoints).Cast<StandingsRowEntity>().ToList(); teamStandings.StandingsRows.ForEach(x => x.ScoringTable = this); //teamStandings.Calculate(); return(teamStandings); }