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
Learn about the partitioning operators in LINQ: Take, Skip, TakeWhile, and SkipWhile.
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
Our next set of operators deal with
partitioning a sequence, like finding and
0:00
extracting a subset of objects.
0:04
Let's peek at
the documentation real quick.
0:08
Partitioning operators.
0:11
We've got take, skip,
takeWhile, and skipWhile.
0:14
So let's switch back to work spaces and
0:21
make sure you've got your
birds in your console.
0:25
I'll make sure I've got mine here.
0:27
If we need a certain number
of objects from a sequence,
0:31
we can use the take operator.
0:34
Birds.Take(3).
0:35
It's more useful when you
order the sequence first.
0:41
Let's get the top three birds
with the shortest name.
0:44
birds.OrderBy b equals
0:47
to b.Name.Length and
0:52
then we'll take three.
0:56
Say we want the next three.
1:01
There's an operator for that.
1:03
We can skip the first three and
then take the next three.
1:04
Birds.OrderBy b goes to b.
1:07
name .length and
1:14
then we'll skip three and
1:18
take three and there's the next three.
1:22
This can be really helpful when browsing
results in a big sequence of objects.
1:27
If we had a thousand birds in our list and
wanted to look at them all.
1:32
It wouldn't be helpful to
print out all of them.
1:37
You wouldn't have enough space.
1:39
So instead we can show
birds three at a time and
1:41
advance to the list with these operators.
1:44
Let's get the next set of three.
1:46
Skip six and take three.
1:50
Well it only gave us two.
1:55
There are no birds left, and
it just returned all it had left.
1:56
The next two operators are similar but
take a predicate.
2:01
Take while will produce all the elements
up until a condition is met.
2:06
It's important to make sure the sequence
is ordered correctly because it will
2:11
iterate through each element to see if the
condition is met, and as soon as it's not,
2:15
it stops, birds.TakeWhile(b
2:19
Goes to b.Named.Length is less than and
six.
2:26
Now that doesn't yield anything,
what's the first bird in our list?
2:34
birds.First.
2:37
All right, it's a cardinal and the name
Cardinal is eight characters long.
2:40
So it fail the condition immediately.
2:45
So if we ordered it by length first.
2:48
Birds.orderBy b goes to b.name.length.
2:51
And then takeWhile b goes
to the same thing above.
2:59
B.name.length.
3:06
Is less than six.
3:09
And now we've got three and they all have
names that are less than six characters.
3:12
There's also the skip while operator.
3:19
You should take a moment
to pause this video and
3:22
write a query using
the skip while operator.
3:24
That's similar to the one we just wrote.
3:27
That skips all the birds until their
name is longer than six characters.
3:29
How did you do?
3:35
All we need to do is change
the operator to a skip while.
3:36
Partitioning operators are useful in
paging results, like with a search engine.
3:45
We'll see an example of this
with our bird data later on.
3:50
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