如何将SubGroup分组以创建大陆列表,其中每个大陆都有自己的县,每个国家/地区都有自己的城市,如此表
这是t-sql:
select Continent.ContinentName, Country.CountryName, City.CityName
from Continent
left join Country
on Continent.ContinentId = Country.ContinentId
left join City
on Country.CountryId = City.CountryId
Run Code Online (Sandbox Code Playgroud)
和t-sql的结果:
我尝试了这个,但它以错误的方式对数据进行分组,我需要像上表一样进行分组
var Result = MyRepository.GetList<GetAllCountriesAndCities>("EXEC sp_GetAllCountriesAndCities");
List<Continent> List = new List<Continent>();
var GroupedCountries = (from con in Result
group new
{
con.CityName,
}
by new
{
con.ContinentName,
con.CountryName
}
).ToList();
List<Continent> List = GroupedCountries.Select(c => new Continent()
{
ContinentName = c.Key.ContinentName,
Countries = c.Select(w => new Country()
{
CountryName = c.Key.CountryName,
Cities = c.Select(ww => new City() …Run Code Online (Sandbox Code Playgroud) 如果对象(国家/地区)不为null,我想返回一个视图.但是我收到错误"Not Not code paths返回一个值"
我的代码看起来像这样
public ActionResult Show(int id)
{
if (id != null)
{
var CountryId = new SqlParameter("@CountryId", id);
Country country = MyRepository.Get<Country>("Select * from country where CountryId=@CountryId", CountryId);
if (country != null)
{
return View(country);
}
}
}
Run Code Online (Sandbox Code Playgroud)