private void FillRelations(IEnumerable <Stadium> stadiums) { IEnumerable <City> cities = new City[0]; if (FillCities) { var citiesDal = new CityDal(); citiesDal.SetContext(Context); var citiesIds = new List <int>(); citiesIds.AddRange(stadiums.Select(r => (int)r.cityId).Distinct()); cities = citiesDal.GetCities(citiesIds).ToList(); } if (cities.Any()) { foreach (Stadium stadium in stadiums) { if (FillCities && cities.Any()) { stadium.city = cities.FirstOrDefault(t => t.Id == stadium.cityId); if (stadium.cityId.HasValue && stadium.city == null) { throw new DalMappingException(nameof(stadium.city), typeof(Stadium)); } } } } }
private void FillRelations(IEnumerable <Person> persons) { if (Guard.IsEmptyIEnumerable(persons)) { return; } IEnumerable <Team> teams = new Team[0]; IEnumerable <City> cities = new City[0]; IEnumerable <PersonRole> personRoles = new PersonRole[0]; IEnumerable <PersonCareer> personCareers = new PersonCareer[0]; if (FillTeams) { var teamDal = new TeamDal(); teamDal.FillCities = true; teamDal.SetContext(Context); var teamIds = new List <int>(); teamIds.AddRange(persons.Where(p => p.teamId.HasValue).Select(p => p.teamId.Value).Distinct()); teams = teamDal.GetTeams(teamIds).ToList(); } if (FillCities) { var cityDal = new CityDal(); cityDal.FillCountries = true; cityDal.SetContext(Context); var cityIds = new List <int>(); cityIds.AddRange(persons.Where(p => p.cityId.HasValue).Select(p => p.cityId.Value).Distinct()); cities = cityDal.GetCities(cityIds).ToList(); } if (FillPersonRoles) { var personRoleDal = new PersonRoleDal(); personRoleDal.SetContext(Context); var personRoleIds = new List <int>(); personRoleIds.AddRange(persons.Where(p => p.roleId.HasValue).Select(p => (int)p.roleId.Value).Distinct()); personRoles = personRoleDal.GetPersonRoles(personRoleIds).ToList(); } if (FillPersonCareer) { var personCareerDal = new PersonCareerDal(); personCareerDal.SetContext(Context); var personIds = new List <int>(); personIds.AddRange(persons.Select(p => p.Id).Distinct()); personCareers = personCareerDal.GetPersonCareer(personIds).ToList(); } if (teams.Any() || cities.Any() || personRoles.Any() || personCareers.Any()) { foreach (Person person in persons) { if (FillTeams && teams.Any()) { person.team = teams.FirstOrDefault(t => t.Id == person.teamId); if (person.teamId.HasValue && person.team == null) { throw new DalMappingException(nameof(person.team), typeof(Person)); } } if (FillCities && cities.Any()) { person.city = cities.FirstOrDefault(c => c.Id == person.cityId); if (person.cityId.HasValue && person.city == null) { throw new DalMappingException(nameof(person.city), typeof(Person)); } } if (FillPersonRoles && personRoles.Any()) { person.role = personRoles.FirstOrDefault(pr => pr.Id == person.roleId); if (person.roleId.HasValue && person.role == null) { throw new DalMappingException(nameof(person.role), typeof(Person)); } } if (FillPersonCareer && personCareers.Any()) { person.PersonCareer = personCareers.Where(pc => pc.personId == person.Id).ToList(); } } } }