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

Gavin Ralston
Gavin Ralston
28,770 Points

LINQ Aggregates: group.Average() returns double in msdn docs, in Workspaces, but not in tutorial Workspace.

birds.GroupBy(b => b.Color).Select(g => new { Color = g.Key, Sightings = g.Sum
(b => b.Sightings) });  

{ { Color = Red, Sightings = 8 }, { Color = White, Sightings = 12 }, { Color = Blue, S
ightings = 1 }, { Color = Yellow, Sightings = 0 }, { Color = Black, Sightings = 11 } }

csharp> birds.Average(b => b.Sightings);   

4.57142857142857 

This tutorial video (approximately @ 3:20) shows you got an int, or probably more likely a floored or truncated result. What gives? :)

1 Answer

Jeremy McLain
STAFF
Jeremy McLain
Treehouse Guest Teacher

You got it. When the C# REPL prints a float that is a whole number it doesn't show the .0. Try 12.0 / 3.0 in the REPL. Carling appears to have been using values that just happened to end up as whole numbers when the average was taken.