private void FillRelations(IEnumerable <PersonStatistics> personStatistics) { IEnumerable <Team> teams = new Team[0]; IEnumerable <Tourney> toureys = new Tourney[0]; if (FillTeams) { var teamDal = new TeamDal(); teamDal.SetContext(Context); var teamIds = new List <int>(); teamIds.AddRange(personStatistics.Select(r => r.teamId).Distinct()); teams = teamDal.GetTeams(teamIds).ToList(); } if (FillTourneys) { var tourneyDal = new TourneyDal(); tourneyDal.SetContext(Context); var tourneyIds = new List <int>(); tourneyIds.AddRange(personStatistics.Select(r => (int)r.tourneyId).Distinct()); toureys = tourneyDal.GetTourneys(tourneyIds).ToList(); } if (teams.Any() || toureys.Any()) { foreach (PersonStatistics ps in personStatistics) { if (FillTeams && teams.Any()) { ps.team = teams.FirstOrDefault(t => t.Id == ps.teamId); if (ps.team == null) { throw new DalMappingException(nameof(ps.team), typeof(PersonStatistics)); } } if (FillTourneys && toureys.Any()) { ps.tourney = toureys.FirstOrDefault(t => t.Id == ps.tourneyId); if (ps.tourney == null) { throw new DalMappingException(nameof(ps.tourney), typeof(PersonStatistics)); } } } } }
private void FillRelations(IEnumerable <Round> rounds) { if (Guard.IsEmptyIEnumerable(rounds)) { return; } IEnumerable <Tourney> tourneys = new Tourney[0]; GameDal dalGames = null; if (FillTourneys) { var tourneyDal = new TourneyDal(); tourneyDal.SetContext(Context); var tourneyIds = new List <int>(); tourneyIds.AddRange(rounds.Select(r => (int)r.tourneyId)); tourneys = tourneyDal.GetTourneys(tourneyIds.Distinct()).ToList(); if (!tourneys.Any()) { throw new DalMappingException(nameof(tourneys), typeof(Round)); } } if (FillProtocols) { FillGames = true; } if (FillGames) { dalGames = new GameDal(); dalGames.FillProtocols = FillProtocols; dalGames.SetContext(Context); } if (tourneys.Any() || dalGames != null) { foreach (Round round in rounds) { if (FillTourneys && tourneys.Any()) { round.tourney = tourneys.FirstOrDefault(t => t.Id == round.tourneyId); if (round.tourney == null) { throw new DalMappingException(nameof(round.tourney), typeof(Round)); } } else { round.tourney = null; } if (dalGames != null) { round.Game = dalGames.GetRoundGames(round.Id).ToArray(); } else { round.Game = null; } } } }