Monday, June 4, 2018

SKIP/TAKE LINQ

Partitioning Operators: Skip & SkipWhile

Partitioning operators split the sequence (collection) into two parts and return one of the parts.
MethodDescription
SkipSkips elements up to a specified position starting from the first element in a sequence.
SkipWhileSkips elements based on a condition until an element does not satisfy the condition. If the first element itself doesn't satisfy the condition, it then skips 0 elements and returns all the elements in the sequence.
TakeTakes elements up to a specified position starting from the first element in a sequence.
TakeWhileReturns elements from the first element until an element does not satisfy the condition. If the first element itself doesn't satisfy the condition then returns an empty collection.

Skip:

The Skip() method skips the specified number of element starting from first element and returns rest of the elements.
Example: Skip() - C#
            
IList<string> strList = new List<string>(){ "One", "Two", "Three", "Four", "Five" };

var newList = strList.Skip(2);

foreach(var str in newList)
    Console.WriteLine(str);

Output:
Three
Four 
Five

Skip operator in Query Syntax:

The Skip & SkipWhile operator is Not Supported in C# query syntax. However, you can use Skip/SkipWhile method on a query variable or wrap whole query into brackets and then call Skip/SkipWhile.
The following example demonstrates skip operator in query syntax - VB.NET
Example: Skip operator in VB.Net

Dim skipResult = From s In studentList
                 Skip 3
                 Select s
                    

SkipWhile:

As the name suggests, the SkipWhile() extension method in LINQ skip elements in the collection till the specified condition is true. It returns a new collection that includes all the remaining elements once the specified condition becomes false for any element.
The SkipWhile() method has two overload methods. One method accepts the predicate of Func<TSource, bool> type and other overload method accepts the predicate Func<TSource, int, bool> type that pass the index of an element.
In the following example, SkipWhile() method skips all elements till it finds a string whose length is equal or more than 4 characters.
Example: SkipWhile in 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

In the above example, SkipWhile() skips first two elements because their length is less than 3 and finds third element whose length is equal or more than 4. Once it finds any element whose length is equal or more than 4 characters then it will not skip any other elements even if they are less than 4 characters.
Now, consider the following example where SkipWhile() does not skip any elements because the specified condition is false for the first element.
Example: SkipWhile in C#
            
IList<string> strList = new List<string>() { 
                                            "Three", 
                                            "One", 
                                            "Two", 
                                            "Four", 
                                            "Five", 
                                            "Six"  };

var resultList = strList.SkipWhile(s => s.Length < 4);

foreach(string str in resultList)
        Console.WriteLine(str);

Output:
Three 
One 
Two 
Four
Five
Six

The second overload of SkipWhile passes an index of each elements. Consider the following example.
Example: SkipWhile with index in C#
            
IList<string> strList = new List<string>() { 
                                            "One", 
                                            "Two", 
                                            "Three", 
                                            "Four", 
                                            "Five", 
                                            "Six"  };

var result = strList.SkipWhile((s, i) => s.Length > i);

foreach(string str in result)
    Console.WriteLine(str);

Output:
Five
Six

In the above example, the lambda expression includes element and index of an elements as a parameter. It skips all the elements till the length of a string element is greater than it's index.

Skip/SkipWhile operator in Query Syntax:

Skip & SkipWhile operator is NOT Supported in C# query syntax. However, you can use Skip/SkipWhile method on a query variable or wrap whole query into brackets and then call Skip/SkipWhile().
Example: SkipWhile method in VB.Net

Dim strList = New List(Of string) From {
                                        "One", 
                                        "Two", 
                                        "Three", 
                                        "Four", 
                                        "Five", 
                                        "Six"  }

Dim skipWhileResult = From s In studentList
                      Skip While s.Length < 4
                      Select s
    
Output:
Three 
Four 
Five 
Six

Partitioning Operators: Take & TakeWhile

Partitioning operators split the sequence (collection) into two parts and returns one of the parts.
MethodDescription
SkipSkips elements up to a specified position starting from the first element in a sequence.
SkipWhileSkips elements based on a condition until an element does not satisfy the condition. If the first element itself doesn't satisfy the condition, it then skips 0 elements and returns all the elements in the sequence.
TakeTakes elements up to a specified position starting from the first element in a sequence.
TakeWhileReturns elements from the given collection until the specified condition is true. If the first element itself doesn't satisfy the condition then returns an empty collection.

Take:

The Take() extension method returns the specified number of elements starting from the first element.
Example: Take() in C#
            
IList<string> strList = new List<string>(){ "One", "Two", "Three", "Four", "Five" };

var newList = strList.Take(2);

foreach(var str in newList)
    Console.WriteLine(str);

Output:
One
Two

Take & TakeWhile operator is Not Supported in C# query syntax. However, you can use Take/TakeWhile method on query variable or wrap whole query into brackets and then call Take/TakeWhile().
Example: Take operator in query syntax VB.Net

Dim takeResult = From s In studentList
                 Take 3
                 Select s

TakeWhile:

The TakeWhile() extension method returns elements from the given collection until the specified condition is true. If the first element itself doesn't satisfy the condition then returns an empty collection.
The TakeWhile method has two overload methods. One method accepts the predicate of Func<TSource, bool> type and the other overload method accepts the predicate Func<TSource, int, bool> type that passes the index of element.
In the following example, TakeWhile() method returns a new collection that includes all the elements till it finds a string whose length less than 4 characters.
Example: TakeWhile in C#
            
IList<string> strList = new List<string>() { 
                                            "Three", 
                                            "Four", 
                                            "Five", 
                                            "Hundred"  };

var result = strList.TakeWhile(s => s.Length > 4);

foreach(string str in result)
        Console.WriteLine(str);

Output:
Three
In the above example, TakeWhile() includes only first element because second string element does not satisfied the condition.
TakeWhile also passes an index of current element in predicate function. Following example of TakeWhile method takes elements till length of string element is greater than it's index
Example: TakeWhile in C#:
            
IList<string> strList = new List<string>() { 
                                            "One", 
                                            "Two", 
                                            "Three", 
                                            "Four", 
                                            "Five", 
                                            "Six"  };

var resultList = strList.TakeWhile((s, i) => s.Length > i);

foreach(string str in resultList)
        Console.WriteLine(str);

Output:
One 
Two 
Three
Four

No comments:

Post a Comment

javascript Filter/index off

 var family = [{"name":"Jack",  "age": 26},               {"name":"Jill",  "age"...