LINQ Skip and SkipWhile in C# Example

Skip and SkipWhile are partitioning operators, that can split the sequence from collection in two parts and return one part, here you learn how to use Skip and SkipWhile in LINQ methods Example.

you must add following required namespace in you class.

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

As we know that Skip and SkipWhile are partitioning operators, these are actually extension methods in LINQ.

Skip and SkipWhile in LINQ C#

Any partitioning operators split the sequence of any collection into two parts and returns one part.

Let's look at some examples of Skip method Skips elements up to the specified position, LINQ Skip() method with integer list

IList<int> intList = new List<int>() { 10, 20, 30, 50, 70, 90, 45, 55 };
IEnumerable<int> _result=  intList.Skip(5);
foreach (int i in _result)
{
    Console.WriteLine(i); 
}

if you execute the above code the result would be all the numbers after skiping first 5 numbers

90 
45
55

Here is another example of LINQ Skip method with a string array

string[] strArray = new string[] { "India", "USA", "UK", "Canada", "Australia" };
IEnumerable<string> _result1 = strArray.Skip(2);

foreach (string s in _result1)
{
    Console.WriteLine(s);
}
LINQ SkipWhile Exmple

Now we look at LINQ SkipWhile method example

SkipWhile method returns elements of any given collection until the specified condition is satisfied.

IList<int> intList = new List<int>() { 10, 20, 30, 50, 70, 90, 45, 55 };
IEnumerable<int> _result= intList.SkipWhile(i=> i <=50 );

foreach (int i in _result)
{
    Console.WriteLine(i);
}

Here is the result, Skip the number where condition (i less than or equal to 50 )

70
90
45
55

Here is another example of LINQ SkipWhile method with a string array

string[] strArray = new string[] { "India", "USA", "UK", "Canada", "Australia" };
IEnumerable<string> _result1 = strArray.SkipWhile(s => s.Length >= 3);

foreach (string s in _result1)
{
    Console.WriteLine(s);
}
UK
Canada<
Australia

Note: We cannot write Skip and SkipWhile operator using query syntax in C#

 
Linq Skip Method Syntax: LINQ SkipWhile C# Example
LINQ (language integrated query) allow you to write query on database objects like ado.net, entity framework etc, LINQ is type safe, easy to convert database object to list objects and business objects in secure and scalable way.
linq Interview Questions Answers
LINQ C# examples | Join Asp.net MVC Course