У меня следующий запрос:
IList<InfrStadium> stadiums =
(from sector in DbContext.sectors
where sector.Type=typeValue
select new InfrStadium(sector.TeamId)
).ToList();
и конструктор класса InfrStadium:
private InfrStadium(int teamId)
{
IList<Sector> teamSectors = (from sector in DbContext.sectors
where sector.TeamId==teamId
select sector)
.ToList<>();
... work with data
}
Текущая реализация выполняет 1 + n запросов, где n - количество записей, выбранных в первый раз.
Я хочу оптимизировать это.
И еще один, который я хотел бы сделать, используя оператор 'group' так:
IList<InfrStadium> stadiums =
(from sector in DbContext.sectors
group sector by sector.TeamId into team_sectors
select new InfrStadium(team_sectors.Key, team_sectors)
).ToList();
с соответствующим конструктором:
private InfrStadium(int iTeamId, IEnumerable<InfrStadiumSector> eSectors)
{
IList<Sector> teamSectors = eSectors.ToList();
... work with data
}
Но попытка запустить запрос вызывает следующую ошибку:
Выражение типа 'System.Int32'
не может быть использован для конструктора
параметр типа
'System.Collections.Generic.IEnumerable`1 [InfrStadiumSector]
Вопрос 1:
Не могли бы вы объяснить, что здесь не так, я не понимаю, почему 'team_sectors' применяется как 'System.Int32'?
Я пытался немного изменить запрос (заменить IEnumerable на IQueryeable):
IList<InfrStadium> stadiums =
(from sector in DbContext.sectors
group sector by sector.TeamId into team_sectors
select new InfrStadium(team_sectors.Key, team_sectors.AsQueryable())
).ToList();
с соответствующим конструктором:
private InfrStadium(int iTeamId, IQueryeable<InfrStadiumSector> eSectors)
{
IList<Sector> teamSectors = eSectors.ToList();
... work with data
}
В этом случае я получил другую, но похожую ошибку:
Выражение типа 'System.Int32'
нельзя использовать для параметра типа
«System.Collections.Generic.IEnumerable 1[InfrStadiumSector]'
of method
'System.Linq.IQueryable
1 [InfrStadiumSector]
AsQueryableInfrStadiumSector '
Вопрос 2:
На самом деле, тот же вопрос: вообще не могу понять, что здесь происходит ...
P.S.
У меня есть другой способ оптимизировать идею запроса (опишите здесь: Linq2Sql: оптимизация запроса ), но я бы хотел найти решение с 1 запросом к БД).