C# Querying With LINQ Query Operators Aggregates

olu adesina
PRO
olu adesina
Pro Student 19,018 Points

where does the g.key property come from in this example

when iterate through the List<> with the FROM and IN keywords we use a variable like b in birds to access properties of each object but there is no Key property in the bird object so where is the g.key property coming from

1 Answer

Steven Parker
Steven Parker
177,847 Points

You're right that a "bird" object doesn't have a "Key", but this is chained onto a GroupBy; and an IGrouping object (which the GroupBy creates) does have a Key. The key was selected by the function given to the GroupBy, so in this case it's the bird's "Color" value.

The "g" parameter name was probably chosen as a reminder that it represents an IGrouping object and not a bird.

olu adesina
olu adesina
Pro Student 19,018 Points

how does c# know to assign g.key to the color property.

Steven Parker
Steven Parker
177,847 Points

Birds.GroupBy(b => b.Color) :point_left: the lambda function selects the color as the Key.