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 the join operators in LINQ: Join and GroupJoin.
LINQ Methods
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
A lot of folks prefer
using query syntax for
0:00
using joins at first because
the method syntax can be pretty tricky.
0:03
Let's write our first
join using query syntax.
0:08
We'll take a list of colors and
0:11
use a join to find all the birds
that have those colors.
0:13
I've still got my birds
here in the console.
0:17
If you need to pause the video and
load them up yourself, make sure to do so.
0:19
We'll start with var colors
0:25
= new list<string>,
0:30
and my favorite colors,
0:35
which are Red, Blue, and Purple.
0:40
Okay, so I'm gonna pick out the birds
that have the colors red, blue, and
0:47
purple, and I'm gonna use a join.
0:52
var favoriteBirds = from b in birds,
0:54
join c in colors, so the c is the range
1:00
variable here in our colors list.
1:06
And then we need on b.Color equals c.
1:12
Now that can be a little tricky for
1:20
me because sometimes I
forget the word equals.
1:22
And I accidentally put in two double
equals, like a normal expression, but
1:24
it's different here in query syntax world.
1:29
And then one more thing,
we need to select the result.
1:32
select, nope, not c,
we'll just select the birds.
1:35
And our favorite birds are Cardinal,
Robin, and Blue Jay.
1:41
So we combined these two sets of objects,
but
1:47
since we only wrote select b,
it returned just the bird objects.
1:49
Let's write the exact same query,
but this time, with methods syntax.
1:55
A join in methods syntax has four
parameters we need to pass to the method,
2:01
the inner sequence, the key from the outer
sequence, the key from the inner sequence,
2:05
and then the result of the join.
2:11
That sounds like a mouthful,
but let's see what I mean.
2:13
The outer sequence is
the first sequence to join.
2:17
The outer sequence can be
anything that uses link or
2:21
any sequence that implements IEnumerable.
2:24
Then the first parameter in
the method is the inner sequence,
2:27
another IEnumerable, and as the sequence
to join the first sequence.
2:30
The outer key selector is a function
that's used on our outer sequence
2:36
to extract the key to join a property
of the objects in the sequence.
2:40
The inner key selector extracts
the key from the inner sequence and
2:46
then the result selector
is a function that
2:51
indicates what we want
to get out of the join.
2:53
We can use lambdas to
specify our outerKeySelector,
2:57
innerKeySelector, and the resultSelector,
since they are all funks.
3:00
Let's write the favorite birds query
again, but using methods syntax.
3:06
I'm gonna clear the console
with Console.Clear.
3:12
var favoriteBirds = birds,
3:18
so that's our outer sequence.
3:22
Join(colors, that's our inner sequence.
3:27
I'm gonna do a line break here.
3:32
b => b.Color, so
that's our outer key selector.
3:35
So I'm using b because we've got birds.
3:42
Then c => c, so
this is our inner key selector.
3:46
And since colors is just
a list of strings and
3:53
not objects, I'm just gonna say c,
which should be a string.
3:56
And then our result selector.
4:02
So here's where it looks a little tricky.
4:04
First we've got two input parameters,
which is the inner sequence and
4:07
then the outer sequence.
4:11
So bird is our outer, it comes first.
4:13
color, so remember when I said that we
could remove the parentheses when we
4:17
only have one parameter?
4:22
Well, we've got two parameters here,
so we have to put both the bird and
4:24
the color here.
4:27
And that's gonna go to, but
4:28
I'm only gonna select bird because
that's how our last query was like.
4:31
Okay, and let's check out my favorite
birds, cardinal, robin, and blue jay.
4:38
Let's try that query again,
but with a different result.
4:44
I'm gonna clear the consul again,
so we can avoid confusion.
4:48
var, favoriteBirds = birds.Join(colors, so
4:53
we've got our outer sequence and
our sequence.
4:58
Now we need our outer key selector,
5:04
b => b.color.
5:09
And c => c, which is our list of colors.
5:12
And then our input parameters,
(bird, color) =>,
5:17
so I'm gonna use an anonymous type here.
5:23
And it's gonna go to a new {
5:27
Color = color, Bird = bird }.
5:32
Okay, let's take a look
at what that looks like.
5:41
So now, we've got an enumerable
of anonymously typed objects, and
5:45
those objects have properties
named color and bird.
5:49
The bird property actually
contains the bird.
5:53
Joins can be pretty tricky
to remember the syntax, but
5:56
with practice, you'll get better at it.
5:59
There's a second method that performs
a join called group join, and
6:03
it performs a group and
a join in the same operation.
6:07
It has the same syntax as a join, but
returns something a bit different.
6:11
Group joins aren't as common
as regular joins, but
6:16
there are a couple of specific scenarios
for which you'll need to break them out.
6:18
We'll take a look at an example
of a common use in a later video.
6:23
For now, let's just check out
the difference in what it returns.
6:27
Let's say, instead of returning a list of
birds, I want the birds grouped by color.
6:31
First, we'll need to switch
the order of our sequences.
6:37
So it's, I wanna group by each color.
6:39
The grouping key must be
the outer sequence in our query.
6:42
var groupedBirds = colors, so
6:45
that's our outer sequence.
6:50
GroupJoin, then (birds will
be our inner sequence.
6:54
Then our outer key selector, c => c,
7:02
our inner key selector, b => b.Color.
7:08
And then outer,
7:14
Inner goes to new {
7:19
Color = color, Birds.
7:25
Notice I use an S there, = bird.
7:33
Okay, let's play around
a bit with this result.
7:40
Remember the select operator?
7:44
Let's use it to select only
the colors in our result.
7:45
groupedBirds.Select(g => g.
7:48
So notice I'm using g, because it's
a grouped bird, and it's easier for
7:57
me to keep up with what variable I'm
doing if it kind of matches what
8:01
your enumerable variable is.
8:06
Select the color.
8:09
That gives us only the colors
in our grouped birds.
8:12
Notice that it returns the color purple,
8:16
even though we don't
have any purple birds.
8:18
Let's see what's inside the birds
property of our group birds.
8:21
groupedBirds.Where(g =>
8:24
g.Color == "Purple") so
8:29
then we'll use the Select operator,
8:33
g => g.Birds.
8:39
Whoops, g.
8:46
Okay, so you see that
the birds collection is empty.
8:51
There's another projection
operator that we can use,
8:56
instead of the select operator.
8:58
The select many operator is like select,
but
9:00
it flattens out a result that
has grouped collections.
9:03
Let's see how it works.
9:07
We've got our grouped birds,
9:09
groupedBirds.SelectMany(g => g.Birds)
9:13
cuz that's an innumerable in our grouping.
9:18
Now we've got our list of grouped birds,
but flattened into a single collection.
9:26
It's kind of like a reverse
operation of a group.
9:31
We just learned how to use joins and
9:35
group joins in Linc, along with a new
projection operator, select many.
9:37
Next up,
we'll be learning about aggregates.
9:42
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