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
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
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
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
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
arrValues2 | String( ) - a String Array = {"#A","#B","#C","A1","B1","C1" }
arrResult = (From x in arrValues2.
Skip While x.StartsWith("#")).ToArray()
arrResult = (From x in arrValues2.
Take While x.StartsWith("#")).ToArray()
Visuals
Samples
Paging a collection
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
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
- tutorialsteacher LINQ Tutorials from Basics to Advanced
- linqsamples https://linqsamples.com
API Documentation
- Enumerable.Skip<TSource>(IEnumerable<TSource>, Int32) Method (System.Linq) | Microsoft Learn
- Enumerable.Take Method (System.Linq) | Microsoft Learn
- Enumerable.SkipWhile Method (System.Linq) | Microsoft Learn
- Enumerable.TakeWhile Method (System.Linq) | Microsoft Learn
Recommendations
Questions
For questions on your specific case open a new topic and get individual support