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.

The Skip Method:

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.

Example1: Using Method syntax

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

Example2: Using Query Syntax

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

Example3: Skip Method with Filtering

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

Example4: Applying Filter after Skip method

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 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.

Example5: 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 with some examples. I hope you understood the need and use of Skip Method in Linq.

Leave a Reply

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