This course will be retired on June 1, 2025.
Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Learn about aggregate operators in LINQ: Sum, Count, Min, Max, and Average.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
In this video we'll be
covering aggregate operators.
0:00
I've still got my birds
list in the REPL here.
0:04
If you need to pause the video and
0:06
follow the instructions in the readme file
to get set up with our list of birds.
0:08
Remember when we use the group
operator and queries syntax.
0:13
We haven't touched on
it using method syntax.
0:17
Let's review how to do that real quick.
0:20
With query syntax we group
birds by color like this.
0:22
From b in birds,
0:26
group b by b.Color.
0:31
In method syntax, here's how we do it.
0:38
Let's group our birds with just
the group by operator first,
0:41
birds.group by
0:44
b goes to b.Color.
0:49
Let's keep using the group by method as we
venture into a new set of link operators
0:54
aggregates.
0:59
Aggregates help us to perform analysis
operations on a dataset, like sum,
1:00
count or average.
1:05
We used the count operator briefly before.
1:07
Let's get a count of birds for
each color group.
1:10
birds.GroupBy b goes to
1:13
b.Color.Select g goes to,
1:18
we'll do an anonymous
1:23
type new Color = g.Key,
1:27
Count equals g.count,
1:32
close curly brace,
1:37
close the method, and enter.
1:41
So there are bird counts for each color.
1:48
Notice that I used another letter g for
the lambda parameter in the select method.
1:51
I could have used a b, but I did that
on purpose so that it's easy for
1:56
me to differentiate that
it's not just a plain bird.
1:59
The objects in that are enumerable,
are now groups of birds,
2:03
let's try another aggregate operator sum.
2:07
We can get the total
number of sightings for
2:10
all the birds birds.Sum,
2:15
b goes to b.Sightings which is an integer.
2:18
We can't just call Sum
without a parameter.
2:24
We need to tell it what
property to sum up.
2:27
Now let's get a sum of
the sightings in each color group.
2:30
birds.Groupby b
2:34
goes to b.Color and
2:38
Select, g goes to new
2:44
Color = g.key and
2:49
Sightings equals g.Sum.
2:54
B goes to b.Sightings curly brace and
3:01
then another close parenthesis.
3:07
We can call the some method on
the sightings collection in that grouping
3:12
because it's also an innumerable.
3:15
The other aggregates are used
in the same way as some.
3:19
birds.Average, b goes to b.Sightings.
3:22
4 is the average, let's get the the min,
3:31
birds.Min b goes to b.Sightings.
3:37
Minimum of zero and one more, max.
3:43
Max(b => b.sightings) and
3:46
11 is our max.
3:53
Check out the teachers notes for
3:58
documentation on a couple more aggregate
operators that are used less often.
3:59
Long count and aggregate.
4:03
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up