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 about conversion operators in LINQ: ToList, and ToArray.
LINQ Conversion Operators
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
Make sure you've got our birds list
loaded in this C# REPL for this video.
0:00
Most of our LINQ operators
return an Enumerable,
0:06
what if we needed to be something
else like a list or an array?
0:09
The one I use most often is ToList,
0:13
birds.Where b goes to
0:17
b.color equals Red and
then I will call ToList.
0:22
Its usefulness might
not be apparent though,
0:31
remember when we talked
about deferred execution.
0:34
Since we're in the C# REPL, our queries
are getting evaluated immediately.
0:37
If we assign a LINQ
expression to a variable,
0:43
var redBirds equals birds.Where
b goes to b.Color equals Red.
0:49
It doesn't get evaluated
until we iterate on it.
0:58
So calling ToList or
any of the other conversion operators
1:04
iterates through the sequence
to produce the result.
1:08
And so our query is executed.
1:11
RedBirds.ToList.
1:13
Similarly, there's ToArray.
1:19
RedBirds.ToArray and now we have an array.
1:21
There's a few more conversion operators
that can be useful in some edge cases,
1:28
like if you need a lookup or
a dictionary.
1:32
I've linked to those in the notes.
1:35
You should check them out and think about
how you could use them in a LINQ query.
1:37
>> Wow,
that's a lot of operators we just used.
1:43
Did you take notes?
1:46
I find myself constantly looking up syntax
and documentation on LINQ operators,
1:47
especially for
ones I don't use very often.
1:52
We learn to use quantifiers
to see if a sequence contains
1:55
elements that fit a condition.
1:59
Element operators to pick single
elements out of a sequence.
2:01
We use partitioning operators to
obtain a subset of a sequence and
2:05
joins to join multiple sequences together.
2:10
We performed aggregations
to analyze our sequences.
2:13
We use set operations to remove duplicates
and merge separate sequences into one.
2:17
We then learned we can use
LINQ to generate sequences and
2:23
finally, to convert them to
different types of sequences.
2:27
We've been working in
workspaces this whole time.
2:31
You'll find that as you advance to
coding in an IDE like Visual Studio.
2:34
The built in features will help you
remember the usage without having
2:39
to look up the documentation.
2:42
Let's take a quick peek
at how using an IDE
2:44
can help us jog our memories
when writing LINQ queries.
2:47
>> I've got a blank console
application here in Visual Studio,
2:52
let's create a list of
numbers like we did earlier.
2:55
Var numbers equals new list of int and
2:58
we'll initialize it, 2,
3:05
4, 8, 16, 32, 64.
3:10
Okay, so
we don't have the System.Linq namespace.
3:18
Let's look at what Visual Studio's
IntelliSense tells us we can call
3:23
on the numbers variable.
3:26
So you'll notice we don't have any of
the LINQ methods we've been using.
3:29
Just the normal list methods, so
3:34
let's add the System.Linq namespace
using System.Linq.
3:38
Now, I'll type a period and it gives us
a lot more methods, look at all those.
3:45
So notice the little
arrow icon right here.
3:54
If I click on this, that arrow is
indicating that it's an extension method.
3:57
And you can see here that
we've got the definition and
4:02
the syntax for each of these.
4:07
Let's call the Where operator,
we've used that a lot.
4:12
Where n goes to n is greater than 10.
4:18
If I hover over the n here,
4:26
notice that it's telling me
the n parameter is an integer.
4:28
How helpful is that?
4:31
It can be really useful
when writing LINQ queries.
4:33
So you see how Visual Studio gives
us a lot of documentation, so
4:36
we don't necessarily have to go and
find it ourselves.
4:40
What a huge help.
4: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