Skip Method in Linq

Skip Method in Linq with Examples

In this article, I am going to discuss the Skip Method in Linq with some examples. Please read our previous article before proceeding to this article where we discussed the TakeWhile 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 is the Skip Method and its need in C#.NET?
  2. Examples of Skip Method using both Method and Query Syntax. 
  3. Using the Skip Method with the Where Filtering Method.
  4. What happens When we Applied the Filtering Operator after the Skip Operator in C#.NET?
  5. Example when the Data source is null.
What the Skip Method is and its need in C#.NET?

The Skip Method in Linq is used to skip or bypass the first “n” number of elements from a data source or sequence and then returns the remaining elements from the data source as output. Here “n” is an integer value passed to the Skip method as a parameter. There is only one version available for this Skip method as shown below.

Skip Method in Linq

As you can see in the above image, the Skip method takes one integer count parameter and this method going to skip that number of elements from the beginning of the data source. If the data source is null then it is going to throw an exception.

Skip Method Using the Method syntax in LINQ:

In the below example, the integer collection contains 10 elements. Here, we need to skip the first four elements and then need to retrieve the remaining elements.

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.Skip(4).ToList();

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

            Console.ReadKey();
        }
    }
}

Output: 5 6 7 8 9 10

Skip Method Using the Query Syntax in LINQ:

Let us rewrite the previous example using the query syntax. There is no such operator called Skip is available to write the query syntax. So, here we need to use the mixed syntax as shown below.

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> ResultQS = (from num in numbers
                                  select num).Skip(4).ToList();

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

            Console.ReadKey();
        }
    }
}

Output: 5 6 7 8 9 10

Understanding the LINQ Skip Method with the Where Filtering Method:

Our requirement is to skip four elements from the collection but the condition is that the elements should be greater than 3. In such cases, first, we need to filter the data using the Where extension method and then we need to skip or bypass the data using the Skip method as shown in the below example.

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 };

            //Using Method Syntax
            int[] ResultMS = numbers.Where(num => num > 3).Skip(4).ToArray();

            //Using Mixed Syntax
            List<int> ResultQS = (from num in numbers
                                  where num > 3
                                  select num).Skip(4).ToList();

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

            Console.ReadKey();
        }
    }
}

Output: 8 9 10

Applying Filter Method after the Skip method in C#.NET:

If you apply the Filtering Operator such as “Where” after the “Skip” method then you will not get the output as expected as shown in the below example.

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 };

            //Using Method Syntax
            int[] ResultMS = numbers.Skip(4).Where(num => num < 7).ToArray();

            //Using Mixed Syntax
            List<int> ResultQS = (from num in numbers
                                  select num).Skip(4).Where(num => num < 7).ToList();

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

            Console.ReadKey();
        }
    }
}

Output: 5 6

As you can see in the above output, we are getting only 2 elements. This is because of the wrong use of Where operator. In this case, first, the “Skip” method is executed which will skip the first four elements and return the remaining six elements and then on this result set the “Where” method is executed which will further filter the data based on the specified condition.

What happens when the Data Source is null?

When we are applying the Skip Method on a data source which is null, then we will get an exception i.e. ArgumentNullException as shown in the below example.

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

namespace LINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //data source is null
            List<int> numbers = null;

            int[] ResultMS = numbers.Skip(4).ToArray();

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

            Console.ReadKey();
        }
    }
}

When you run the application, you will get the following exception.

Linq Skip method throwing Argument Null Exception

In the next article, I am going to discuss the SkipWhile Method in Linq with some examples. In this article, I try to explain the Skip Method in LINQ with some examples. I hope you understood the need and use of the Linq Skip Method.

Leave a Reply

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