C
C#3w ago
Faker

✅ Looping through collections with key value pairs

Hello guys, consider the following code:
C#
// percentileQuery is an IEnumerable<IGrouping<int, Country>>
var percentileQuery =
from country in countries
let percentile = (int)country.Population / 1_000
group country by percentile into countryGroup
where countryGroup.Key >= 20
orderby countryGroup.Key
select countryGroup;

// grouping is an IGrouping<int, Country>
foreach (var grouping in percentileQuery)
{
Console.WriteLine(grouping.Key);
foreach (var country in grouping)
{
Console.WriteLine(country.Name + ":" + country.Population);
}
}
C#
// percentileQuery is an IEnumerable<IGrouping<int, Country>>
var percentileQuery =
from country in countries
let percentile = (int)country.Population / 1_000
group country by percentile into countryGroup
where countryGroup.Key >= 20
orderby countryGroup.Key
select countryGroup;

// grouping is an IGrouping<int, Country>
foreach (var grouping in percentileQuery)
{
Console.WriteLine(grouping.Key);
foreach (var country in grouping)
{
Console.WriteLine(country.Name + ":" + country.Population);
}
}
I'm a bit confused about how grouping is used in the loops. The thing is I first tried to compare it with how we loop for a dictionary but in a dictionary, we would use grouping.Value in the inner loop but here, we used grouping in both loops. What's happening here please, how does grouping know which one is an int or which one is the collection.
11 Replies
Sehra
Sehra3w ago
IGrouping = Key + IEnumerable
canton7
canton73w ago
IGrouping inherits from IEnumerable but adds a Key property (it's not an object with a Key property and another property which is the IEnumerable)
We used grouping in both loops
Not really? In the first loop you're looping over percentileQuery, and calling each element grouping. In the second loop you're looping over grouping and calling each element country
MODiX
MODiX3w ago
Angius
REPL Result: Success
var u = new Unga<int>("hello") { 1, 2, 3, 76, 9 };

foreach (var x in u)
{
Console.WriteLine($"{u.Bunga}: {x}");
}

public class Unga<T>(string bunga) : List<T>
{
public string Bunga { get; set; } = bunga;
}
var u = new Unga<int>("hello") { 1, 2, 3, 76, 9 };

foreach (var x in u)
{
Console.WriteLine($"{u.Bunga}: {x}");
}

public class Unga<T>(string bunga) : List<T>
{
public string Bunga { get; set; } = bunga;
}
Console Output
hello: 1
hello: 2
hello: 3
hello: 76
hello: 9
hello: 1
hello: 2
hello: 3
hello: 76
hello: 9
Compile: 606.226ms | Execution: 69.156ms | React with ❌ to remove this embed.
Angius
Angius3w ago
Similar example
Faker
FakerOP3w ago
Yeah I see, so IGrouping<> is just an IEnumerable kind of? Just that each element in it has a key property?
Angius
Angius3w ago
No, the grouping itself has a key property Just like my Unga is a List<T> but it also has a Bunga property
Faker
FakerOP3w ago
ahhh yeah I see, that's why it's not possible to say something like x.Bunga but rather u.Bunga?
Angius
Angius3w ago
Yeah, because x here is an int And int does not have a property called Bunga
Faker
FakerOP3w ago
Yeahh I see I think it's clearer now will come back if I have other doubts but should be good for now, thanks guys !!
Angius
Angius3w ago
:Ok:

Did you find this page helpful?