public Team Update(Team entity) { _context.Entry(entity).State = EntityState.Modified; _context.SaveChanges(); return(entity); }
/// <summary> /// <para>Load all data required for calculating the statistics from the database.</para> /// <para>Must be called prior to <see cref="Calculate"/> if lazy loading is disabled!</para> /// </summary> /// <param name="dbContext">Database context from EntityFramework.</param> /// <param name="force">Force loading data again even if IsDataLoaded is true.</param> public virtual async Task LoadRequiredDataAsync(LeagueDbContext dbContext, bool force = false) { if (DriverStatistic == null || DriverStatistic.Count == 0) { await dbContext.Entry(this) .Collection(x => x.DriverStatistic) .LoadAsync(); var memberIds = DriverStatistic.Select(x => x.MemberId); var sessionIds = DriverStatistic .SelectMany(x => new long?[] { x.FirstSessionId, x.LastSessionId, x.FirstRaceId, x.LastRaceId }) .Where(x => x != null) .Select(x => x.Value); var resultRowIds = DriverStatistic .SelectMany(x => new long?[] { x.FirstResultRowId, x.LastResultRowId }) .Where(x => x != null) .Select(x => x.Value); await dbContext.Set <LeagueMemberEntity>() .Where(x => memberIds.Contains(x.MemberId)) .LoadAsync(); await dbContext.Set <SessionBaseEntity>() .Where(x => sessionIds.Contains(x.SessionId)) .LoadAsync(); await dbContext.Set <ResultRowEntity>() .Where(x => resultRowIds.Contains(x.ResultRowId)) .LoadAsync(); dbContext.ChangeTracker.DetectChanges(); } }
public Player Update(Player entity) { _context.Entry(entity).State = EntityState.Modified; _context.SaveChanges(); return(entity); }
/// <summary> /// <para>Load all data required for calculating the statistics from the database.</para> /// <para>Must be called prior to <see cref="Calculate"/> if lazy loading is disabled!</para> /// </summary> /// <param name="dbContext">Database context from EntityFramework.</param> /// <param name="force">Force loading data again even if IsDataLoaded is true.</param> /// <returns></returns> public override async Task LoadRequiredDataAsync(LeagueDbContext dbContext, bool force = false) { if (IsDataLoaded && force == false) { return; } await base.LoadRequiredDataAsync(dbContext, force); // Load season statistic sets await dbContext.Entry(this).Collection(x => x.StatisticSets).LoadAsync(); // Load data for each SeasonStatisticSet foreach (var statisticSet in StatisticSets) { await statisticSet.LoadRequiredDataAsync(dbContext); } IsDataLoaded = true; }
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); }
public override async Task LoadRequiredDataAsync(LeagueDbContext dbContext, bool force = false) { if (IsDataLoaded && force == false) { return; } await base.LoadRequiredDataAsync(dbContext, force); if (Season == null) { await dbContext.Entry(this) .Reference(x => x.Season) .LoadAsync(); } //if (DriverStatistic == null || DriverStatistic.Count == 0) //{ // await dbContext.Entry(this) // .Collection(x => x.DriverStatistic) // .LoadAsync(); //} if (ScoringTableId == null || ScoringTableId == 0) { return; } var scoringTable = dbContext.Set <ScoringTableEntity>() .Where(x => x.ScoringTableId == ScoringTableId) .Include(x => x.Scorings.Select(y => y.ExtScoringSource)) //.Include(x => x.Scorings.Select(y => y.Sessions.Select(z => z.SessionResult))) //.Include(x => x.Scorings.Select(y => y.ScoredResults.Select(z => z.FinalResults.Select(q => q.ResultRow.Member)))) //.Include(x => x.Scorings.Select(y => y.ExtScoringSource.ScoredResults.Select(z => z.FinalResults.Select(q => q.ResultRow.Member)))) .FirstOrDefault(); var loadScoringEntityIds = dbContext.Set <ScoringEntity>().Local.Select(y => y.ScoringId); if (loadScoringEntityIds.Count() > 0) { var loadScorings = dbContext.Set <ScoringEntity>() .Where(x => loadScoringEntityIds.Contains(x.ScoringId)) .Include(x => x.Sessions.Select(y => y.SessionResult)) .ToList(); var loadScoredResults = dbContext.Set <ScoredResultEntity>() .Where(x => loadScoringEntityIds.Contains(x.ScoringId)) .Include(x => x.FinalResults.Select(y => y.ResultRow.Member)) .Include(x => x.CleanestDrivers) .Include(x => x.HardChargers) .ToList(); } var scorings = scoringTable.Scorings; foreach (var scoring in scorings) { await dbContext.Entry(scoring) .Reference(x => x.ConnectedSchedule) .Query() .Include(x => x.Sessions) .LoadAsync(); await dbContext.Entry(scoring) .Collection(x => x.Sessions) .LoadAsync(); scoring.GetAllSessions(); } var scoringIds = scorings.Select(y => y.ScoringId); await dbContext.Set <ScoredResultEntity>() .Where(x => scoringIds.Contains(x.ScoringId)) .Include(x => x.FinalResults.Select(y => y.AddPenalty)) .Include(x => x.FinalResults.Select(y => y.ReviewPenalties)) .LoadAsync(); var resultIds = scorings.SelectMany(x => x.ScoredResults.Select(y => y.ResultId)); await dbContext.Set <ResultEntity>() .Where(x => resultIds.Contains(x.ResultId)) .Include(x => x.RawResults.Select(y => y.Member.Team)) .Include(x => x.IRSimSessionDetails) .Include(x => x.Session.Reviews.Select(y => y.AcceptedReviewVotes.Select(z => z.CustomVoteCat))) .LoadAsync(); dbContext.ChangeTracker.DetectChanges(); IsDataLoaded = true; }
public void Update(object entity) { var entityState = db.Entry(entity); entityState.State = EntityState.Modified; }