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
Dive into LINQ and write your first query.
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
Here we are in workspaces.
0:00
You can follow along by opening up
workspaces by clicking the button below.
0:02
We're going to use the C# REPL and
the console of our workspace here.
0:06
To launch, we will enter csharp and
press Enter.
0:10
The first thing we need to do is make
sure we've got the system.link namespace.
0:16
In the C# REPL we can just enter,
using system.link.
0:20
It says we've already got a directive for
it.
0:30
That's because it includes the using
directives for system, system.link,
0:32
system.collections and
system.collections.generic by default.
0:37
Let's start out with a list of integers.
0:42
List<int> numbers =
0:45
new List<int> { 2,
0:50
4, 8, 16,
0:56
32, 64 };.
0:59
If you haven't seen a list before, a list
is a type of generic collection in C#.
1:03
When a type is generic, it means that
it's designed to handle any type.
1:09
So we can declare a list and
put whatever we want into it.
1:13
But we have to tell it what
to expect when we declare it.
1:17
We've passed int as the type
parameter to the list.
1:20
Then we're instantiating a new list and
initializing it with some integers.
1:25
If you haven't seen this before,
it's similar to initializing an array.
1:30
We don't have to do it that way but it's
a nice short cut to save us some time.
1:35
We'll get into some more object
initialization later on.
1:39
It's important to note that the list
collection implements I innumerable of T,
1:43
which means we can use Linc with it.
1:48
Say we need to only get the integers out
of our numbers list that are greater
1:51
than 10.
1:54
How would we go about that?
1:55
Well, we would write a for
or a for each loop.
1:56
First we need to create a new
list to store our result.
1:59
Then we'd write the for each loop that
would iterate through our numbers list.
2:03
Inside the loop,
2:07
we'd write an expression that would check
if the current number fits our criteria.
2:08
And if it does we'd add
it to the new list.
2:12
Once loop is done, we've only got numbers
that are greater than 10 in our new list.
2:15
Let's see how we would do that.
2:19
Declare a new list.
2:22
numbersGreaterThanTen =
2:24
new List<int>();.
2:29
Then our for
each loop foreach(int number in
2:33
numbers).</int> our expression.
2:39
If the number is indeed
2:47
greater than 10,
2:52
then numbers greater than
2:57
ten.Add(number).
3:03
And then our number's greater than ten.
3:08
Should only have 16, 32 and 64.
3:13
Now let's do the same thing
using a link query expression.
3:17
from number in numbers where
3:23
the number is greater than
3:29
10, select number;.
3:34
And that also returned 16, 32 and 64.
3:39
We just wrote a link query that
did the same thing as our for
3:43
each loop but was way faster to write and
easier to read.
3:47
And our original numbers
list wasn't modified.
3:51
Our query just returned a new collection.
3:55
Writing that query may feel a little
different than what you might
3:59
be used to in C#.
4:02
If you've worked with SQL,
the clauses FROM, WHERE and
4:04
SELECT might be familiar to you, but
4:07
the words have moved around a little
bit from how you'd write them in SQL.
4:09
Let's talk about how we wrote
that statement a little more.
4:14
A link query is composed of a range
variable, a source, a query, and a result.
4:18
Looking at our query expression we just
wrote, the range variable is this part,
4:25
from number.
4:30
Then we have our source, and
numbers, and then our query,
4:33
where number > 10, and
finally our result, select number.
4:37
The range variable part
might be a little confusing.
4:43
Why did we have to do that?
4:46
Its job is to be a reference
inside the query.
4:48
Take a look at our previous
example with the for each loop.
4:51
We have to declare an int number, so
4:54
we could refer to it inside the for
each loop.
4:57
That's exactly what a range
variable is doing from number.
5:00
In fact, I could call it anything I want,
from xyz.
5:05
I named it number though
to be more descriptive.
5:10
Some developers prefer to use a single
letter as the range variable name.
5:13
And it's generally okay to do that
when you deal with variables that
5:17
have such a small scope
as with link queries.
5:20
You'll probably see both out in the wild.
5:24
Let's see how readable this is.
5:26
from N in numbers
5:28
where N is greater
5:33
than 10 select n.
5:37
A lot of developers like to increase
readability even more by separating
5:42
out each component of the link
query onto its own line like this.
5:47
from n in numbers where
n is greater than 10.
5:51
Select n.
5:58
We can see that it's returning
a sequence of integers, but
6:03
is it returning a list of
integers like we want?
6:06
Let's assign the result of
our query to a variable.
6:09
List of int numbersGreaterThanTen
6:13
Equals from N in numbers
6:21
where n is greater than
6:26
10, select N.
6:31
Ha, we got an error cannot explicitly
convert type System.Collections.Generic.IE
6:35
numerable to
system.collections.generic.list.
6:42
Let's change it to IEnumerable then and
see if that works.
6:46
IEnumerable numbersGreaterThanTen = and
6:51
one more time a feeling from numbers.
6:58
Whoops, did that wrong.
7:05
Let's cancel out of there, and
7:07
IEnumerable<int> numbersGreaterThanTen
7:12
= from n in numbers</int>
7:19
Where N is greater than 10.
7:24
Select n.
7:28
Link queries always return
an enumerable of the type that was in
7:33
the original collection.
7:36
This is important because it means that
the link query isn't evaluated until
7:38
we enumerate over it.
7:42
This is called deferred execution.
7:44
The statement isn't evaluated until we
iterate through it with a for each or
7:46
use a method on it like count
that forces it to be evaluated.
7:50
So let's do that.
7:54
NumbersGreaterThanTen.Count().
7:55
But when we're here in the REPL,
8:03
our statements are automatically
being evaluated.
8:05
We'll see some more examples of deferred
execution later on in this course.
8:07
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