Asp.net mvc 仅当您访问该接口时,才会加载该接口。因此GetCountries().WithId(1)将对数据库进行1次调用,GetCountries().WithId(1)将对数据库进行2次调用。您的版本在任何情况下都会发出最大数量的db调用,还是我误解了什么?
仅当您访问该接口时,才会加载该接口。因此GetCountries().WithId(1)将对数据库进行1次调用,GetCountries().WithId(1)将对数据库进行2次调用。您的版本在任何情况下都会发出最大数量的db调用,还是我误解了什么?Asp.net mvc 仅当您访问该接口时,才会加载该接口。因此GetCountries().WithId(1)将对数据库进行1次调用,GetCountries().WithId(1)将对数据库进行2次调用。您的版本在任何情况下都会发出最大数量的db调用,还是我误解了什么? ,asp.net-mvc,linq,stack-overflow,Asp.net Mvc,Linq,Stack Overflow,仅当您访问该接口时,才会加载该接口。因此GetCountries().WithId(1)将对数据库进行1次调用,GetCountries().WithId(1)将对数据库进行2次调用。您的版本在任何情况下都会发出最大数量的db调用,还是我误解了什么? public ActionResult ShowCountry(int id) { Country country = _gameService.GetCountry(id); return View
public ActionResult ShowCountry(int id)
{
Country country = _gameService.GetCountry(id);
return View(country);
}
public Country GetCountry(int id)
{
return _gameRepository.GetCountries().WithCountryId(id).SingleOrDefault();
}
public IQueryable<Country> GetCountries()
{
var countries = from c in _db.Countries
select new Country
{
Id = c.Id,
Name = c.Name,
ShortDescription = c.ShortDescription,
FlagImage = c.FlagImage,
Game = GetGames().Where(g => g.Id == c.GameId).SingleOrDefault(),
SubRegion = GetSubRegions().Where(sr => sr.Id == c.SubRegionId).SingleOrDefault(),
};
return countries;
}
public IQueryable<Game> GetGames()
{
var games = from g in _db.Games
select new Game
{
Id = g.Id,
Name = g.Name
};
return games;
}
public IQueryable<SubRegion> GetSubRegions()
{
return from sr in _db.SubRegions
select new SubRegion
{
Id = sr.Id,
Name = sr.Name,
ShortDescription = sr.ShortDescription,
Game = GetGames().Where(g => g.Id == sr.GameId).SingleOrDefault(),
Region = GetRegions().Where(r => r.Id == sr.RegionId).SingleOrDefault(),
Countries = new LazyList<Country>(GetCountries().Where(c => c.SubRegion.Id == sr.Id))
};
}