[HowTo] LINQ - First Start # Skip | Take | SkipWhile | TakeWhile Operator

This HowTo gives an introductory overview of the partition Operators: Skip, Take, SkipWhile, TakeWhile

Introduction

The partition operators are used to fetch a particular subset from a set of items. The returned subset is formed by the contiguous items that are matching the provided condition.

Skip / Take Operator

The Skip Operator will omit the subsequent items from the given start for a given length and will return the remaining items

The Take Operator will return the subsequent items from a given start for a given length

Overview

I – Input P – Processing O – Output
{"#A:#B", "A1:B1", "A2:B2" } exclude the first 1 item(s) {"A1:B1","A2:B2"}
{"#A:#B", "A1:B1", "A2:B2" } grab the Top 1 item(s) {"#A:#B"}

Implementation

Method Syntax

arrValues | String( ) - a String Array =  {"#A:#B", "A1:B1","A2:B2" }
arrResult = arrValues.Skip(1).ToArray()
arrResult = arrValues.Take(1).ToArray()
Visuals

grafik

Reminder:

  • Using these LINQ Operators the returned set of items can have a count of 0,1 or more.
  • The returned LINQ generic baselined datatype will be transformed into a targeted datatype - in this case a String Array - so ToArray()
Visuals

grafik

Query Syntax

arrValues | String( ) - a String Array =  {"#A:#B", "A1:B1","A2:B2" }
arrResult = (From x in arrValues 
             Skip 1).ToArray()

arrResult = (From x in arrValues 
             Take 1).ToArray()
Visuals

grafik

SkipWhile / TakeWhile Operator

The SkipWhile Operator will omit the subsequent items from the given start as long a provided expression is matched and returns the remaining items.

The TakeWhile Operator will return the subsequent items from the given start as long a provided expression is matched.

Overview

I – Input P – Processing O – Output
{"#A","#B","#C","A1","B1","C1" } exclude the top sequence items as long the item value starts with a β€˜#’ { "A1", "B1", "C1" }
{"#A","#B","#C","A1","B1","C1" } grab the top items as long the item value starts with a β€˜#’ { "#A", "#B", "#C" }

Implementation

Method Syntax

arrValues2 | String( ) - a String Array = {"#A","#B","#C","A1","B1","C1" }
arrResult =  arrValues2.SkipWhile(Function (x) x.StartsWith("#")).ToArray()
arrResult = arrValues2.TakeWhile(Function (x) x.StartsWith("#")).ToArray()
Visuals

grafik

For the evaluation when the TakeWhile/SkipWhile is to stop an expression will be defined within the function (x) <EXPRESSION> block. The provided part has to return a mandatory boolean result.

Query Syntax

The SkipWhile/TakeWhile Operator is not available as a building block in the Query Syntax. But a Query Syntax statement can be combined with an appended SkipWhile/TakeWhile operator as method syntax. As a result, the strength of both can be utilized.

arrValues2 | String( ) - a String Array = {"#A","#B","#C","A1","B1","C1" }
arrResult =  (From x in arrValues2.
              Select x.Trim()).SkipWhile(Function (x) x.StartsWith("#")).ToArray()
arrResult =  (From x in arrValues2.
              Select x.Trim()).TakeWhile(Function (x) x.StartsWith("#")).ToArray()

Samples

Paging a collection

grafik
arrValues3.Skip(0*SegmentSize).Take(SegmentSize).ToArray()

Datatable Segmentation

Given segment size is used for calculating the number of segments a data table can be sliced. With the combined Skip and Take operator the data table will be segmented accordingly.

SegmentSize = 3
NoOfSegments = CInt(Math.Ceiling(dtData.Rows.Count / SegmentSize))
ListTables | List(of DataTable) = 
Enumerable.Range(0,NoOfSegments).Select(Function (x) dtData.AsEnumerable.Skip(x*SegmentSize).Take(SegmentSize).CopyToDataTable).toList
Visuals

grafik


Kindly note: Last Segment is incomplete (less then segment size of 3, but the take Operator is no failing

Omit CSV Header Line

arrLinesFiltered =  File.ReadAllLines(YourFilePath).Skip(1).ToArray

Summary

  • with the Skip/SkipWhile Operators items can be omitted from a given start and the remaining items are returned
  • with the Take/TakeWhile Operators subsequent items are returned
  • Using these LINQ Operators the returned set of items can have a count of 0,1 or more
  • If needed the returned LINQ generic baselined datatype can be transformed into a targeted datatype by using available ToXX methods
  • For SkipWhile/TakeWhile Operators the provided evaluation condition has to return a boolean

References

API Documentation

Recommendations

  • Training courses from Academy
  • Debugging course from Academy (Highly recommended)

Questions

For questions on your specific case open a new topic and get individual support

2 Likes