I am trying to select countries with the name of at least one of their cities in a different (supplied) list. Sorry it's hard to explain, please see the code below:
When I call GetListOfCountires, it should return NZ and CN. I also want to use Linq instead of foreach.
private static List<Country> Countries = new List<Country>(); private static void Main() { var city1 = new City {Name = "Auckland"}; var city2 = new City { Name = "Wellington" }; var city3 = new City { Name = "Perth" }; var city4 = new City { Name = "Sydney" }; var city5 = new City { Name = "Beijing" }; var country1 = new Country {Name = "NZ", Cities = new List<City> {city1, city2}}; var country2 = new Country { Name = "AU", Cities = new List<City> { city3, city4 } }; var country3 = new Country { Name = "CN", Cities = new List<City> { city5 } }; Countries.Add(country1); Countries.Add(country2); Countries.Add(country3); List<String> cityNames = new List<string>{"Auckland", "Beijing"}; var countries = GetListOfCountires(cityNames);
thanks
source share