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