C# Querying With LINQ Query Operators Aggregates

Gavin Ralston
Gavin Ralston
28,765 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.