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 how to write LINQ queries in method syntax using lambda expressions.
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
Under the hood at runtime,
all of the operators and link query syntax
0:00
such as from, where, select,
an order by, are converted to methods.
0:05
Here, we've got a list of all
the methods that are exposed
0:10
as part of the link.innumerable type.
0:13
I've included this link in the notes.
0:16
Link is made possible by a feature
of C sharp called extension methods.
0:19
Extension methods give us the ability
to tack on methods to classes that we
0:23
wouldn't otherwise be able to alter.
0:27
Well learn about writing our own
extension methods a little later.
0:30
When we add the system dot
link namespace to our project.
0:34
These methods are added to classes that
implement the I innumerable interface,
0:37
like list and array.
0:42
Let's check out the where
method Control F and where
0:43
the first parameter of an extension
method is the object is being called on,
0:53
which in this case is an I
enumerable down here.
0:58
The keyword is indicating that
it's an extension method.
1:02
Filters a sequence of values based
on a predicate, what does that mean?
1:08
If we look at the definition
on those where method,
1:13
you can see that there's two parameters.
1:15
An IEnumerable and a funk.
1:18
Since this is an extension method,
the first parameter,
1:21
Source, is the collection
it's being called from.
1:24
The second parameter,
called the predicate,
1:28
is a funk that tells where method
how to filter the enumerable.
1:31
The term predicate in programming, is
a test case, it gets evaluated to true or
1:36
false.
1:41
So, the where method needs a predicate
delegate to tell it how to test
1:42
the innumerable,
it specifies it needs a func type
1:46
that returns a boolean and we learn that
we can write func delegates with a lambda.
1:51
Let's dive back into our birdwatching
data and write some more queries.
1:56
We can start using link method syntax and
lambda expressions.
2:01
We'll be working with the C# again, so
2:05
click the workspaces
button to follow along.
2:07
We need to load up our
birdwatcher assembly, so
2:10
we can get our list of birds back.
2:12
If you get lost, there's a readme document
in workspaces for you to refer to.
2:14
Start the rebel with C sharp and
2:19
then load assembly, bird watcher dot dll,
2:24
and then using birdwatcher,
2:32
which is the namespace.
2:36
And then var birds
equals=BirdRepository.loadBirds.
2:41
Okay.
2:51
Let's revisit our first wear query,
2:52
where we wanted a list of all
birds that are the color red.
2:55
From B in birds,
2:59
where the B dot color
3:03
equals red select B.
3:08
Okay, now let's write this
query using method syntax and
3:14
a lambda expression Birds.where,
3:17
where b is the input parameter.
3:22
Goes to b.Color == "Red".
3:27
Hah.
3:38
Notice that we don't have
to tell it what to select.
3:38
It's inferred from the list of birds
that we're calling the where method on.
3:41
The other thing you should notice
is that I didn't include brackets
3:46
around the lambda expression.
3:50
This is a more syntactic sugar for
us to keep things as concise as possible.
3:53
We could've written it with
the brackets like this.
3:58
Birds.where b
4:01
goes to curly brace return
4:07
to b.color goes to red.
4:13
Semi colon, curly brace,
close parentheses.
4:18
But since there's only one
statement inside the brackets,
4:24
we can leave them out.
4:26
We also don't need to use the parentheses
around the input parameter.
4:28
We can do it like birds.Where,
4:32
b goes to b.Color equals Red.
4:36
Since we only have one parameter,
the parentheses is optional.
4:44
We'll get into link methods later on
that take more than one parameter.
4:47
Let's revisit a query similar to one we
wrote earlier with queries untaxed to
4:52
order the list of birds by color.
4:56
I'll clear the consul here.
4:58
Clear.
5:02
Okay, from B-N birds.
5:03
Order by be taught color select B.
5:08
Remember that?
5:14
To do these using methods syntax,
we need to use the ORDER BY method.
5:16
Just like the wear method,
5:20
we need to tell it to order
the birds with a lambda expression.
5:22
Birds dot order.
5:26
He goes to and will give it
the property that we want to order by
5:28
it which is color, and
we get the same results.
5:34
If we need to order in reverse,
5:40
there's an additional method
called orderByDescending.
5:42
birds.OrderByDescending.
5:45
b goes to b.Color.
5:50
So, what would we do if we need to
filter and order at the same time?
5:56
We can use method chaining to call both
the where method and the order method.
6:02
Birds.where, b goes to
be.color equals red,
6:07
close that, and then .OrderBy
6:15
b goes to b.order by name this time.
6:20
So, the order in which you
call the methods is important,
6:29
the final result type will be the result
of the last method in the chain.
6:32
Think on how we would go about ordering
with multiple properties with methods
6:37
syntax with query syntax,
we separated the properties with a comma.
6:41
But in method syntax, we need to
use an additional method called,
6:46
then by when we use order by,
6:49
were returned an ordered enumerable which
gives us access to the then by method.
6:53
Let's try that out.
6:58
Birds dot order by,
7:00
B goes to B dot name,
7:04
then by Bn goes to B dot sightings.
7:09
Why do you have to use the then by method,
and not another order by method?
7:17
Well, the then by method will
preserve the order of the list
7:21
that we got as a result of
the initial order by method.
7:25
So if we called ORDER BY twice
like this bird's dot order by
7:28
B goes to B dot name and then order by
7:35
B goes to B dot sightings,
7:42
we end up with the entire collection
being sorted by sightings.
7:49
So, far our queries have been
returning a new marbles of birds.
7:54
So, what if we want to innumerable
of anonymous objects and not birds.
7:57
You guessed it.
8:01
There's a method called Select.
8:02
birds.Select, b goes to new,
8:04
b.Name and b.Color,
8:09
close parenthesis And
8:14
now we have anonymous types.
8:18
I really like method syntax with link
because it's so short and sweet.
8:25
You'll find that developers may be
divided on which syntax they prefer, and
8:30
that's okay.
8:34
You are free to choose
whichever one you prefer, but
8:35
it's good to get comfortable
with both styles.
8:39
If you're working on a project
that's mostly using query syntax,
8:42
you should do the same to be consistent,
you can even mix the two styles.
8:46
We've covered the where, order by,
then by and the select methods.
8:51
Check out the teachers notes for
the documentation on each of those.
8:55
We'll be covering a lot of new
methods in the next few videos.
8:59
So I don't courage you to take
notes as we move through each one.
9:02
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