SkipWhile Method in Linq

SkipWhile Method in Linq with Examples

In this article, I am going to discuss the SkipWhile Method in Linq with some examples. Please read our previous article before proceeding to this article where we discussed the Linq Skip Method with some examples. As part of this article, we are going to discuss the following pointers related to the Linq Skip Method.

  1. What the SkipWhile Method is and its need in C#.NET?
  2. Examples of SkipWhille Method using both Method and Query Syntax. 
  3. Example using Index.

What the SkipWhile Method is and its need in C#.NET?

The SkipWhile Method in Linq is used to skip or bypass all the elements from a data source or sequence as long as the given the condition specified by the predicate is true and then returns the remaining element from the sequence as an output.

The most important point that you need to remember is, once the condition is failed then the SkipWhile method will not check rest of the elements even though the condition is true for some of the remaining elements.

There are two overloaded versions available for this method as shown in the below image.

SkipWhile Method in Linq

The First overloaded version of the SkipWhile method Bypasses the elements in a sequence as long as a specified condition is true and then returns the remaining elements as an output.

The second overloaded version of this method bypass or skips the elements from a sequence as long as the given condition is true and then returns the remaining elements. The second parameter of the function represents the index of the source elements. The element’s index can be used in the logic of the predicate function. If this is not clear at the moment then don’t worry we will discuss this with an example.

Example1:

In the below example, we created a data source which contains numbers from 1 to 10. Then we are fetching the elements from the collection using the SkipWhile method with the condition that number is less than 5.

using System;
using System.Collections.Generic;
using System.Linq;

namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            List<int> ResultMS = numbers.SkipWhile(num => num < 5).ToList();

            foreach (var num in ResultMS)
            {
                Console.Write($"{num} ");
            }

            Console.ReadKey();
        }
    }
}

Output: 5 6 7 8 9 10

Example2:

Let us modify the position of the elements in the collection. Here we are moving the elements 2 and 3 to the end of the collection.

using System;
using System.Collections.Generic;
using System.Linq;

namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> numbers = new List<int>() { 1, 4, 5, 6, 7, 8, 9, 10, 2, 3 };

            List<int> ResultMS = numbers.SkipWhile(num => num < 5).ToList();

            foreach (var num in ResultMS)
            {
                Console.Write($"{num} ");
            }

            Console.ReadKey();
        }
    }
}

Output: 5 6 7 8 9 10 2 3

As you can see we are getting 2 and 3 in the output even they are smaller than 5. This is because the SkipWhile method checks the conditions from the beginning of the data source. As long as the condition is true it bypasses the data. So, in our example, for the first two elements, the condition is true so it skips the first two elements. When it checks the third elements the condition becomes false. So from that point, it will not check the rest of the elements in the data source even though some of the elements (i.e. 2 and 3 present at the end of the collections) satisfying the condition.

Example3:

In the following example, we have a collection of names. Here, we need to skip the names’ starting from the beginning whose length is less than 4.

using System;
using System.Collections.Generic;
using System.Linq;

namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> names = new List<string>() { "Pam", "Rahul", "Kim", "Sara", "Priyanka" };

            List<string> namesResult = names.SkipWhile(name => name.Length < 4).ToList();

            foreach (var name in namesResult)
            {
                Console.Write($"{ name} ");
            }

            Console.ReadKey();
        }
    }
}

Output: Rahul Kim Sara Priyanka

Example4: Using Index

Let us use the other overloaded version of the SkipWhile method which takes the index position of the element as a parameter. Here, in the following example, we will check the length of the name should be greater than its index position.

using System;
using System.Collections.Generic;
using System.Linq;

namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> names = new List<string>() { "Sara", "Rahul", "John", "Pam", "Priyanka" };

            List<string> namesResult = names.SkipWhile((name, index) => name.Length > index).ToList();

            foreach (var name in namesResult)
            {
                Console.Write($"{ name} ");
            }
            
            Console.ReadKey();
        }
    }
}

Output: Pam Priyanka

In the sequence the fourth element i.e. Pam has length 3 and its index position is 3. So, here the condition is false and hence it will not check the next element. As a result, it only fetches the last two elements from the sequence.

In the next article, I am going to discuss how to implement paging using the Skip and Take Method with an example. Here, in this article, I try to explain the LINQ SkipWhile Method with some examples. I hope you understood the need and use of the SkipWhile method in Linq.

Leave a Reply

Your email address will not be published. Required fields are marked *