✅ Looping through collections with key value pairs
Hello guys, consider the following code:
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
IGrouping Interface (System.Linq)
Represents a collection of objects that have a common key.
IGrouping = Key + IEnumerable
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 loopsNot 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
Angius
REPL Result: Success
Console Output
Compile: 606.226ms | Execution: 69.156ms | React with ❌ to remove this embed.
Similar example
Yeah I see, so
IGrouping<>
is just an IEnumerable
kind of? Just that each element in it has a key
property?No, the grouping itself has a key property
Just like my
Unga
is a List<T>
but it also has a Bunga
propertyahhh yeah I see, that's why it's not possible to say something like
x.Bunga
but rather u.Bunga
?Yeah, because
x
here is an int
And int
does not have a property called Bunga
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 !!
:Ok: