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
Let's build some LINQ queries to get familiar with our new data set.
This video doesn't have any notes.
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
Our workspaces has all the new and
improved classes for us.
0:01
You can follow along in the example,
0:05
just like we did before,
with the same steps,
0:10
csharp, LoadAssembly("BirdWatcher.dll"),
0:16
and using BirdWatcher,
0:24
then we'll load up our birds,
0:27
var birds = BirdRepository.LoadBirds.
0:32
Okay, let's explore our data a little
by getting some record counts.
0:40
Let's see how many birds we're
dealing with here, birds.Count.
0:44
That's a decent number.
0:50
Now, how many sightings do we have?
0:52
Each bird has a list of sightings, so
0:55
we can perform a select on all
the birds to get all the sightings and
0:59
then call the count method, birds.Select,
1:07
b goes to b.Sightings, and Count.
1:13
Wait, that can't be right.
1:18
There's no way that each
bird only has one sighting.
1:19
I forgot to use select many,
the select method returned to us
1:23
an innumerable of sighting sequences for
each bird.
1:28
So we just counted how many collections
of sightings there are total, and
1:32
not the sightings themselves.
1:36
Let's try that again,
this time using select many to
1:38
flatten out the sightings
into one collection,
1:43
birds.SelectMany, where
b goes to b.Sightings,
1:48
and then we'll call count.
1:54
Now that's better.
2:00
A decent number for us to work with.
2:01
Let's see, what's the average
sighting count per bird in our data?
2:03
Well, we can use the total
number of sightings and
2:08
then put it over the total
number of birds, right?
2:10
Nine, well, you know there's
a better way we can do that,
2:16
we can use the average method instead.
2:20
So, since we want the average
of the total count per bird,
2:22
we should use select to get a sequence
of the total counts, you follow?
2:27
Select b goes to b.Sightings.Count,
2:34
and then we'll call Average.
2:39
That worked, and it looks like it
was a little more accurate too.
2:48
Well, now I'm wondering how many different
countries we have bird sighting data in.
2:51
Let's see, so
to get to the country of each sighting,
2:56
we'll need to access the place
property of sighting.
3:00
Well, you select many first to
get a flat list of sightings,
3:03
birds.SelectMany, b goes to b.Sightings.
3:10
So, that'll give us a big
collection of all the sightings.
3:16
Then, we need to get each habitat on each
sighting, not habitat, I think it's place.
3:21
Let's check just to make sure.
3:29
So on the sighting, class,
yep, it's called place.
3:32
So, we'll need to call a select, where s,
3:39
I'll use s,
since we're dealing with sightings now,
3:44
goes to s.Place, and then .Country, so
3:50
that should return all of our countries.
3:55
Well, I got an extra space here.
4:03
All right, now that we've got
a giant list of country strings,
4:06
we can use distinct to get a list
of the distinct countries.
4:09
So I'll press up to get my last command,
and I'll call Distinct.
4:13
All right, that's a little better.
4:21
Now, let's see how many
sightings are in each country.
4:22
Usually when I hear the phrase,
in each, or the word, by,
4:26
as in count of sightings by country,
I know that I'll need to use a grouping.
4:30
Let's start out with all our sightings,
4:35
birds.SelectMany, b goes to b.Sightings,
4:40
then we'll need a GroupBy, so .GroupBy,
4:47
and then we'll group by the country, so
4:52
that's GroupBy, s goes to s.Place.Country.
4:57
All right, let's see what that gives us.
5:06
Wow [LAUGH], so we've got a bunch
of groups of sighting objects,
5:08
and the key should be countries, so
now to get the final count by country,
5:13
we'd need to perform a select, and
then create an anonymously typed object.
5:18
So, I'll tack on a Select method,
5:23
Select, and
I'll say group instead of a letter.
5:29
We're gonna do an anonymous type here,
so new,
5:33
curly brace, and the country,
should be the grp.Key,
5:38
and then the Sightings is what we'll
call the account, and grp.Count.
5:44
Okay, let's see what I did there.
5:56
Got an extra space again.
6:03
Let's try that.
6:07
Okay, so it looks like we've got,
Canada's got 200,
6:09
the US has 233,
200 in pretty much each country.
6:14
Feel free to play around a little more and
write some link queries for
6:19
our bird data before we
get to the next video.
6:23
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