I have an object hierarchy arranged as Continents > Countries > Cities. Am able to select all the cities in a specific “country” as below. What I am looking for is a way to merge these two queries, and arrive at the cityList in a single query.
var cities = network.Continents
.SelectMany(continent => continent.Countries)
.Where(ctry => ctry.Id == "country")
.SelectMany(ctry => ctry.Cities);
List<City> cityList = (from c in cities
select new City { Id = c.Id, Name = c.Name }).ToList<City>();
The “c in cities” have a different structure from the one in cityList and hence the mapping in the second query.
Just use dot notation in your query:
I think it’s readable, and it doesn’t have extra overhead; usually, the generated SQL query is similar to what you may write on your own, hence the readability of this one is its advantage.