C
C#4w ago
Faker

✅ Nested from clause in query syntax

C#
var largeCitiesList = (
from country in countries
from city in country.Cities
where city.Population > 10000
select city
).ToList();

// or split the expression
IEnumerable<City> largeCitiesQuery =
from country in countries
from city in country.Cities
where city.Population > 10000
select city;
var largeCitiesList2 = largeCitiesQuery.ToList();
C#
var largeCitiesList = (
from country in countries
from city in country.Cities
where city.Population > 10000
select city
).ToList();

// or split the expression
IEnumerable<City> largeCitiesQuery =
from country in countries
from city in country.Cities
where city.Population > 10000
select city;
var largeCitiesList2 = largeCitiesQuery.ToList();
Hello guys, can someone explain the use of the nested from clause in the code above please. Why do we use nested from? Couldn't we just use the dot notation, like country.Cities? Why would it matter here?
8 Replies
ACiDCA7
ACiDCA74w ago
you have a list of countries... every country has a list of cities.. you want every city that has a population > 10000 what you have written or copied is called a linq query. its specifically for querying datastructures (and db cough)
jcotton42
jcotton424w ago
It's "flattening" the cities into one sequence. So you end up with one list of cities, instead of a list of lists of cities. In method syntax this would be
var largeCities = countries
.SelectMany(country => country.Cities)
.Where(city => city.population > 10000)
.ToList();
var largeCities = countries
.SelectMany(country => country.Cities)
.Where(city => city.population > 10000)
.ToList();
Angius
Angius4w ago
Equivalent in regular LINQ would be
var largeCitiesList = countries
.SelectMany(c => c.Cities)
.Where(c => c.Population > 10_000)
.ToList();
var largeCitiesList = countries
.SelectMany(c => c.Cities)
.Where(c => c.Population > 10_000)
.ToList();
Ninja'd
Faker
FakerOP3w ago
yep I see hmm I was trying to know how the structure would be with a single from clause, say we want to select only countries, so we have something like
var query = (from country in countries
select country).ToList();
var query = (from country in countries
select country).ToList();
Does the list become something this:
List<Country>
[
{
Name = "USA",
Cities =
[
{ Name = "New York", Population = 8419600 },
{ Name = "Los Angeles", Population = 3980400 },
{ Name = "Smalltown", Population = 8000 }
]
},
{
Name = "Canada",
Cities =
[
{ Name = "Toronto", Population = 2930000 },
{ Name = "Vancouver", Population = 631000 }
]
}
]
List<Country>
[
{
Name = "USA",
Cities =
[
{ Name = "New York", Population = 8419600 },
{ Name = "Los Angeles", Population = 3980400 },
{ Name = "Smalltown", Population = 8000 }
]
},
{
Name = "Canada",
Cities =
[
{ Name = "Toronto", Population = 2930000 },
{ Name = "Vancouver", Population = 631000 }
]
}
]
I'm a bit confused because of the curly braces... I know we use them for object initializer but here we want to represent them in the list, is it actually how it appears internally ?
jcotton42
jcotton423w ago
var query = (from country in countries
select country).ToList();
var query = (from country in countries
select country).ToList();
is the same as
var query = countries.ToList();
var query = countries.ToList();
Also, where are you getting that from?
Faker
FakerOP3w ago
oh ok I asked chatGPT to show me how it would appear but I guess it was wrong :c but hmm I have a question, consider this from Microsoft Docs:
C#
City[] cities = [
new City("Tokyo", 37_833_000),
new City("Delhi", 30_290_000),
new City("Shanghai", 27_110_000),
new City("São Paulo", 22_043_000)
];

//Query syntax
IEnumerable<City> queryMajorCities =
from city in cities
where city.Population > 30_000_000
select city;

// Execute the query to produce the results
foreach (City city in queryMajorCities)
{
Console.WriteLine(city);
}

// Output:
// City { Name = Tokyo, Population = 37833000 }
// City { Name = Delhi, Population = 30290000 }

// Method-based syntax
IEnumerable<City> queryMajorCities2 = cities.Where(c => c.Population > 30_000_000);
// Execute the query to produce the results
foreach (City city in queryMajorCities2)
{
Console.WriteLine(city);
}
// Output:
// City { Name = Tokyo, Population = 37833000 }
// City { Name = Delhi, Population = 30290000 }
C#
City[] cities = [
new City("Tokyo", 37_833_000),
new City("Delhi", 30_290_000),
new City("Shanghai", 27_110_000),
new City("São Paulo", 22_043_000)
];

//Query syntax
IEnumerable<City> queryMajorCities =
from city in cities
where city.Population > 30_000_000
select city;

// Execute the query to produce the results
foreach (City city in queryMajorCities)
{
Console.WriteLine(city);
}

// Output:
// City { Name = Tokyo, Population = 37833000 }
// City { Name = Delhi, Population = 30290000 }

// Method-based syntax
IEnumerable<City> queryMajorCities2 = cities.Where(c => c.Population > 30_000_000);
// Execute the query to produce the results
foreach (City city in queryMajorCities2)
{
Console.WriteLine(city);
}
// Output:
// City { Name = Tokyo, Population = 37833000 }
// City { Name = Delhi, Population = 30290000 }
I tried it on my IDE and I obtained the output mentioned, which is correct but the thing is, why do we have the {} I'm confused about that
C#
record City(string Name, long Population);
record Country(string Name, double Area, long Population, List<City> Cities);
record Product(string Name, string Category);
C#
record City(string Name, long Population);
record Country(string Name, double Area, long Population, List<City> Cities);
record Product(string Name, string Category);
Oh ok that's because City was declared using record ?
jcotton42
jcotton423w ago
Yes. record supplies a ToString that does that.
Faker
FakerOP3w ago
Yep I see, it's clearer now, thanks !!

Did you find this page helpful?