This course will be retired on July 14, 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
Well done!
You have completed Querying With LINQ!
You have completed Querying With LINQ!
Preview
Learn about ordering and grouping in LINQ queries.
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
Let's get back to LINQ queries.
0:00
I'll need to load our list of birds again,
but if you've already got yours
0:02
in your console from the last video,
you won't need to do this.
0:06
csharp, LoadAssembly
0:09
("BirdWatcher.dll").
0:14
using Birdwatcher; and var,
0:21
we're gonna use the var key word again,
0:25
= BirdRepository.LoadBirds().
0:30
Okay, we've got a lot of
birds in our birds list.
0:35
Let's get them in some kind of order.
0:39
To do that,
we'll use the order by keyword.
0:42
From B in birds
0:45
orderby b.Name.
0:49
Select b.Name.
0:55
If you want to get the order reversed,
1:00
you can use the word descending
after the property like this,
1:04
from b in birds orderby b.Name descending,
select b.Name.
1:12
If we want to order by
multiple properties,
1:21
we can separate them with a comma.
1:24
So to get all birds ordered by color but
1:26
then sightings in reverse order,
1:32
we could write,
from b in birds order by b.Color,
1:37
b.Sightings descending,
then select b.Name.
1:44
Well, that just gives us the names.
1:52
Let's use anonymous types again and
get a list of names and sightings.
1:56
Use the up arrow key
to get some lines from
2:01
b in birds orderby b.Color and
2:07
b.Sightings descending,
2:13
select new { b.Name, B.Sightings.
2:17
So now you can see that our bird colors
are in ascending order with Crow,
2:27
that's black first, and
our red birds, Robin and
2:32
Cardinal, are in descending
order according by sighting.
2:35
So five and then three.
2:39
Now let's talk about grouping.
2:43
Grouping is often used when you
need to organize a collection by
2:44
a common attribute.
2:47
It's kind of like filing.
2:49
You need to put all the paperwork for
one person into the same folder so
2:51
it's easier to access.
2:54
There are two new clauses we'll need,
group and into.
2:56
When you group a query result,
you get a new sequence of type I grouping.
3:00
Let's start with a grouping by a string,
how about color?
3:05
var birdsByColor
3:11
= from b in birds
3:16
group b by b.Color.
3:21
Notice that I didn't
use the select clause.
3:28
Let's see what the type is.
3:30
birdsByColor.GetType.
3:32
Okay, so it's a group to numerable which
means that instead of one set of birds,
3:38
we now have different sets of birds for
each color.
3:43
Each grouping has a key, the color, and
3:47
then the collection of
birds that have that color.
3:49
It's most often used with aggregations
like as we've seen the count method.
3:52
So we can access the collection
of birds by key like this.
3:57
foreach( var bird in birdsByColor)
4:02
Console.WriteLine (bird.Key),
4:12
which should be color,
4:18
and bird.Count.
4:25
So there's our birds, grouped by color,
and how many birds are in each group.
4:32
We can also use the into
keyword in our query.
4:38
It's helpful because it lets us use
the group result in a where clause.
4:41
Let's try that out.
4:45
From b in birds group b
4:47
by b.Color into whoops.
4:52
Let's try that again,
4:59
from b in birds group b by
5:04
b.Color into birdsByColor.
5:08
So it's actually a new range variable
that we can use in our where class.
5:14
where birds by color.Count > 1,
5:20
select new { Color = birdsByColor.Key,
5:26
and Count = birdsByColor.Count() }; and
5:33
now we have two anonymously typed
5:40
objects one with the color red,
5:45
and the count of birds that are red.
5:51
And another with the color white and
the count of birds that are white.
5:56
We'll be getting into some more grouping
later when we talk about aggregates.
6:00
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