English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

LINQ Partition Operators Skip & SkipWhile

The partition operator divides the sequence (set) into two parts and returns one of them.

MethodDescription
Skip

Start from the first element of the sequence and jump the element to the specified position.

SkipWhile

Skip elements based on a condition until an element does not meet the condition. If the first element itself does not meet the condition, it will skip 0 elements and return all elements in the sequence.

Take

Start from the first element of the sequence and move the element to the specified position.

TakeWhile

Returns elements from the first element until the element does not meet the condition. If the first element itself does not meet the condition, it returns an empty collection.

Skip

The Skip() method skips a specified number of elements starting from the first element and returns the remaining elements.

IList<string> strList = new List<string>(){ "One", "Two", "Three", "Four", "Five" };
var newList = strList.Skip(2);
foreach(var str in newList)
    Console.WriteLine(str);
Output:
Three
Four
Five

The Skip operator in query syntax

C# query syntax does not support Skip & SkipWhile operators. However, you can use Skip on query variables/SkipWhile method, or enclose the entire query in parentheses and then call Skip/SkipWhile

The following example demonstrates the skip operator in query syntax-VB.NET version

Dim skipResult = From s In studentList
                 Skip 3
                 Select s

SkipWhile

As the name implies, the SkipWhile() extension method in LINQ skips elements in a collection until the specified condition is true. Once any element's specified condition becomes false, it returns a new collection containing all remaining elements.

SkipWhile() method has two overloads. One method accepts a predicate of type Func<TSource, bool>, and another overload accepts a predicate of type Func<TSource, int, bool> that takes the element index.

In the following example, SkipWhile() method skips all elements until an element with a length equal to or greater than4characters.

Example: SkipWhile() method skips strings with a length less than4elements, and returns all elements after it

IList<string> strList = new List<string>() { 
                                            "One", 
                                            "Two", 
                                            "Three", 
                                            "Four", 
                                            "Five", 
                                            "Six"
var resultList = strList.SkipWhile(s => s.Length < 4);
foreach(string str in resultList)
        Console.WriteLine(str);
Output:
Three
Four
Five
Six

In the above example, since the lengths of the first two elements are less than3is found, so SkipWhile() skips the first two elements and finds an element with a length equal to or greater than4the third element. Once an element with a length equal to or greater than4any element with a length of less than4characters.

Now, let's look at the following example, in which SkipWhile() does not skip any elements because the specified condition for the first element is false.

IList<string> strList = new List<string>() { 
                                            "Three", 
                                            "One", 
                                            "Two", 
                                            "Four", 
                                            "Five", 
                                            "Six"
var resultList = strList.SkipWhile(s => s.Length < 4);
foreach(string str in resultList)
        Console.WriteLine(str);
Output:
Three
One
Two
Four
Five
Six

The second overload of SkipWhile passes the index of each element. See the following example

Example: Using SkipWhile() method with index in C#
IList<string> strList = new List<string>() { 
                                            "One", 
                                            "Two", 
                                            "Three", 
                                            "Four", 
                                            "Five", 
                                            "Six"
var result = strList.SkipWhile((s, i) => s.Length > i);
foreach(string str in result)
    Console.WriteLine(str);
Output:
Five
Six

In the above example, the lambda expression includes both the element and its index as parameters. It skips all elements until the length of the string element is greater than its index.

SkipWhile operator in query syntax

C# query syntax does not support Skip & SkipWhile operators. However, you can use Skip on query variables/SkipWhile method, or enclose the entire query in parentheses and then call Skip/SkipWhile ()。

Example: SkipWhile method in VB.Net
Dim strList = New List(Of string) From {
                                        "One", 
                                        "Two", 
                                        "Three", 
                                        "Four", 
                                        "Five", 
                                        "Six"
Dim skipWhileResult = From s In studentList
                      Skip While s.Length < 4
                      Select s
Output:
Three
Four
Five
Six