public async Task <CountryWithCitiesVM> FindCountry(Guid id) { if (id == null || string.IsNullOrWhiteSpace(id.ToString())) { return(null); } var country = await _db.Countries .Include(x => x.Cities) .SingleOrDefaultAsync(x => x.Id == id); if (country == null) { return(null); } CountryWithCitiesVM countryVM = new CountryWithCitiesVM { Cities = country?.Cities ?? null, }; country.Cities = null; countryVM.Country = country; return(countryVM); }
public async Task <List <CountryWithCitiesVM> > AllCountries() { //List<CityWithCountryVM> citiesVM = new List<CityWithCountryVM>(); //foreach (var item in cities) //{ // CityWithCountryVM city = new CityWithCountryVM() // { // CountryId = item.Country?.Id, // CountryName = item.Country?.Name ?? "Stateless", // People = item.People ?? null // }; // item.People = null; // item.Country = null; // city.City = item; // citiesVM.Add(city); try { var countries = await _db.Countries .Include(x => x.Cities) .ToListAsync(); if (countries == null || countries.Count == 0) { throw new Exception(); } List <CountryWithCitiesVM> countriesVM = new List <CountryWithCitiesVM>(); foreach (var item in countries) { var country = new CountryWithCitiesVM { Cities = item.Cities }; item.Cities = null; country.Country = item; countriesVM.Add(country); //countriesVM.Countries.Add(new CountryWithCitiesVM() { Country = item, Cities = item.Cities }); } return(countriesVM); } catch { return(null); } }