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