Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Querying With LINQ Query Operators Aggregates

olu adesina
olu adesina
23,007 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
231,268 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
23,007 Points

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

Steven Parker
Steven Parker
231,268 Points

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