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 the LINQ element operators: Single, First, Last, and ElementAt.
LINQ Element Operator 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
So far we've been using operators
that return an Enumerable.
0:00
The next set of operators deal
with returning a single element
0:04
from a collection.
0:07
They're used quite often in LINQ queries.
0:09
The Single method will return
a single element from our collection.
0:12
We can use it without a parameter,
like in combination with the Where method.
0:16
birds.Where, and don't forget,
if you don't have birds,
0:21
load up your birds using
the instructions in Workspaces.
0:25
Where b goes to b.Name == Crow.
0:29
And then we'll call .Single.
0:38
And there's our crow.
0:42
Or we can use the predicate
as a parameter.
0:45
birds.Single b goes to b.Name
0:48
== Crow, and there it is.
0:54
What happens if we use the Single method
but there isn't a match in the collection?
0:59
birds.Single where b goes to
1:05
b.Name == Chickadee.
1:10
I don't think there's
a chickadee in there.
1:14
Ouch.
1:17
Invalid operation exception.
1:18
Sequence contains no matching element.
1:21
There's another version of this method,
and all of the element operators in LINQ
1:23
have this version of their method,
SingleOrDefault.
1:27
So let's try that one again,
but instead of just Single,
1:32
I'll tack on OrDefault.
1:37
And then it returns null.
1:41
So what this method does is if it doesn't
find a match, it returns a default value.
1:43
So in this case,
a bird has a default value of null.
1:49
If we were dealing with integers, say,
1:54
a list like we were using before,
1:58
var numbers = new List of int 2, 4, 8.
2:01
All right, that's enough.
2:06
And if we use SingleOrDefault to try and
get a number that doesn't exist,
2:10
Numbers.SingleOrDefault where
2:14
n goes to n == 99,
2:20
then it returns 0 since the default
value for an integer is 0.
2:25
So what happens if we use
the Single method but
2:30
our predicate matches more than one bird?
2:33
birds.Single, burned again,
InvalidOperationException.
2:36
Sequence contains more than one element.
2:43
So the Single method
will return an exception
2:46
if the condition returns more than
one element from the sequence.
2:49
When we use Single,
2:53
we want to make sure that there's only
one of its kind in the collection.
2:54
You might think, well,
I should use SingleOrDefault so
2:59
that I don't get an exception.
3:01
It depends on what the situation is.
3:03
You might want an exception if you
are truly looking for an unique object in
3:05
the collection and there's more than one,
or it doesn't exist.
3:09
The next two methods are pretty similar
to each other, the First method and
3:14
the Last method.
3:19
They can be used the same way as Single,
but
3:20
it's not expected that
the element is unique.
3:23
Let's get the first bird in our list.
3:25
birds.First.
3:27
And what's the last bird in our list?
3:32
birds.Last.
3:33
We can use a predicate just
like with the single method.
3:36
birds.First where b
3:40
goes to b.Color = Red.
3:44
And like the single method,
if there are no elements that match,
3:51
we get an exception.
3:54
So let's try that again with Chickadee.
3:58
These methods also have the or
default versions.
4:02
So if we're not sure an element exists,
we'll use FirstOrDefault.
4:06
Default.
4:13
Whoops, forgot the Or.
4:17
FirstOrDefault.
4:18
Null, and we don't get an exception.
4:20
The last method we'll talk
about is the ElementAt method.
4:24
We can use it to get an element at
a certain position in the sequence.
4:28
So like with using an indexer on an array,
4:32
int of numbers = 0, 1, 2, 3.
4:37
And I can access with an indexer like.
4:44
But we can also use LINQ
to use ElementAt(2) and
4:49
it does the same thing.
4:55
I personally haven't seen
the ElementAt method used much, but
5:00
I'm sure there's some edge
case where it might be handy.
5:04
If your Enumerable is also a list,
you can still use the indexer, so
5:07
like birds[0].
5:11
But if you've got the result of a LINQ
query, which is a pure enumerable, so
5:16
var redBirds = birds.Where b
5:21
goes to b.Color == Red.
5:26
We wouldn't be able to use
an indexer to access the items.
5:33
redBirds.
5:37
Nope.
5:40
But you could still use
the ElementAt method.
5:41
redBirds.ElementAt(0).
5:42
And just like the other
methods of this type,
5:50
we can use OrDefault to avoid an exception
if the element doesn't exist.
5:52
So redBirds.ElementAt, we'll do 99.
5:58
But if we used OrDefault,
6:01
just null.
6:09
Let's recap these methods.
6:13
Single, use when
the element must be unique.
6:15
Exception if more than one,
or none exists.
6:18
First, use if there could be more than
one, but you only need the first.
6:22
Exception if no match.
6:26
Last, use if there could be more than one,
but you only need the last.
6:28
Exception if no match.
6:32
ElementAt, use if you know
the exact position of the element.
6:34
Exception if no match.
6:38
SingleOrDefault, use only when the element
must be unique, or it may not exist.
6:40
FirstOrDefault, use if there could
be more than one or none, but
6:47
you only need the first.
6:50
LastOrDefault, use if there
could be more than one or none,
6:53
but you only need the last.
6:56
And ElementAtOrDefault, use if you know
the exact position of the element, but
6:58
it might not be there.
7:03
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