自己的城市,如該表LINQ:分組亞組
這裏如何組亞組創建大洲每個大洲都有它自己的縣名單和每個國家都有的是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
和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()
{
CityName = ww.CityName
}
).ToList()
}).ToList()
}).ToList();
它以錯誤的方式分組?它以何種方式錯誤? –
我甚至沒有在T-SQL中看到一個組。 – juharr
我想你必須在第一組的投影中重新組合:'Result.GroupBy(x => x.ContinentName).Select(g => g.GroupBy(x => x.CountryName))''。 –