| by Arround The Web | No comments

LINQ Skip Operator

The LINQ Skip operator is used to return the remaining values by skipping the values from the given data structure. It takes an integer value as a parameter that represents the total number of elements to be skipped from the given data structure.

Syntax:

input_source.Skip(n);

Or

from element in input_source select element.Skip(n)

Where input_source is the data source.

Parameters:

It takes an integer value(n) that skips that number of elements from the given data structure from first and returns the remaining elements.

Now, consider a scenario where the value of n(integer) is greater than the total number of elements in the data structure, all the elements in the data structure are skipped without any error.

Example 1:

Here, we will create a list that has 10 integers and skip 5 elements using the Skip operator using both the methods(Method and Query).

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

 //create a class - Linuxhint
class Linuxhint
{

    static public  void Main(){
   
    //create List named input
    var input = new  List() {34,56,78,12,34,53,56,78,90,100};
   
    //return remaining integers by skipping 5 values
    var result=input.Skip(5);
    Console.WriteLine("---Using Method---");
    //display the result
  foreach (int i in result)  
            {  
Console.WriteLine(i);  
            }  
   
    Console.WriteLine("---Using Query---");
   
    //return remaining integers by skipping 5 values
    foreach (int j in (from  element in input select element).Skip(5))  
            {  
Console.WriteLine(j);  
            }  
   
    }
   
}

Output:

Explanation:

1. First, we created a list named input_numbers that hold 10 integer elements.

2. After that, we are skipping 5 values using Skip() with Method syntax.

3. Finally, we are displaying the remaining values using a foreach loop.

4. Similarly, we are displaying remaining values by skipping 5 values using Query Syntax.

Example 2:

Here, we will create a string array that has 4 strings and skip 6 elements using the Skip operator using both the methods(Method and Query).

using System;
using System.Linq;

 //create a class - Linuxhint
class Linuxhint
{

    static public  void Main(){
   
    //create string array named input
    string[] input =  {"Linuxhint","java","html","sravan"};
   
    //skip all strings
    var result=input.Skip(6);
    Console.WriteLine("---Using Method---");
    //display the result
  foreach (string i in result)  
            {  
Console.WriteLine(i);  
            }  
   
    Console.WriteLine("---Using Query---");
   
    //display the result by skipping all strings.
    foreach (string j in (from  element in input select element).Skip(6))  
            {  
Console.WriteLine(j);  
            }  
   
    }
   
}

Output:

So, you can see that all the elements are skipped.

Explanation:

Create a string array named input that holds 4 strings.

Use Method syntax to skip all values using the Skip() operator.

Use Query syntax to skip all values using the Skip() operator.

Conclusion

We have seen how to skip the elements using Skip() operator in C# – LINQ. It takes an integer value(n) that skips that number of elements from the given data structure from first and returns the remaining elements.

In each example we implemented Skip() with Method as well as Query syntax. Finally, we came to notice that if the value of n(integer) is greater than the total number of elements in the data structure, then all the elements in the data structure are skipped without any error.

Share Button

Source: linuxhint.com

Leave a Reply