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 trialolu adesina
23,007 Pointswhere 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
231,275 PointsYou'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
23,007 Pointsolu adesina
23,007 Pointshow does c# know to assign g.key to the color property.
Steven Parker
231,275 PointsSteven Parker
231,275 PointsBirds.GroupBy(
b => b.Color
)
the lambda function selects the color as the Key.olu adesina
23,007 Pointsolu adesina
23,007 Pointsthank you kind sir