Saturday, August 15, 2020

Partitioning Operator: Skip & SkipWhile

 

In LINQ, Partition Operators are used for separating the given sequence into two portions without

Sorting the element and return the one of the portions


Method

Description

Skip

This operator is used to skip the specified number of elements in a sequence return the remaining elements.

SkipWhile

This operator is used to skip the elements in a sequence based on the condition, Which is defined as true

 

Example: Skip() Method – C#

 

IList<string> strList = new List<string>(){"Hindi""Marathi""Math""Science"};

 

var newList = strList.Skip(2);

 

foreach(var str in newList)

    Console.WriteLine(str);

 

 

Output -

Math

Science

 

Example: SkipWhile() Method – C#

 
IList<string> strList = new List<string>() { 
                                            "One", 
                                            "Two", 
                                            "Three", 
                                            "Four", 
                                            "Five", 
                                            "Six"  };
 
var resultList = strList.SkipWhile(s => s.Length < 4);
 
foreach(string str in resultList)
        Console.WriteLine(str);

 



Output -

Three
Four
Five
Six


No comments:

Post a Comment