TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Take and TakeWhile Examples

Use the Take and TakeWhile extension methods to get the first several elements of an IEnumerable.
Take. Take returns the first specified number of elements. It acts upon an IEnumerable sequence. It returns another IEnumerable sequence containing the specified elements.
Method details. Take returns the first elements of an IEnumerable. And TakeWhile, a more complex form of Take, accepts the first elements while a predicate method returns true.ExtensionSkip, SkipWhile
Example. We use the Take method. Take is a method in the System.Linq namespace that allows you to get the first several elements from a sequence. It receives an element count.LINQ

Part A: This Take call returns the first 2 elements in the List. This could display the 2 oldest, first elements.

Part B: The second Take call is chained after a Reverse<string> call. It operates on the result of Reverse.

Reverse

Part C: The final Take call uses the top 1000 elements, which is nonsense as there are not 1000 elements in the List.

List
C# program that uses Take using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { List<string> list = new List<string>(); list.Add("cat"); list.Add("dog"); list.Add("programmer"); // Part A: get first 2 elements. var first = list.Take(2); foreach (string s in first) { Console.WriteLine(s); } Console.WriteLine(); // Part B: get last 2 elements reversed. var last = list.Reverse<string>().Take(2); foreach (string s in last) { Console.WriteLine(s); } Console.WriteLine(); // Part C: get first 1000 elements. var all = list.Take(1000); foreach (string s in all) { Console.WriteLine(s); } Console.WriteLine(); } } Output cat dog programmer dog cat dog programmer
TakeWhile. This method returns elements while a Predicate matches. The Predicate returns true or false. When it returns false, TakeWhile returns.

Info: TakeWhile operates from the beginning of an IEnumerable collection. It can be called on the result of another method like Skip.

Predicate

Part 1: An integer array of 5 elements is declared. The first 3 numbers in it are odd, while the last 2 are even.

Odd, Even

Part 2: The TakeWhile extension method is invoked. It returns the first 3 odd numbers, but not the even numbers at the end.

LambdaModulo
C# program that uses TakeWhile method using System; using System.Linq; class Program { static void Main() { // Part 1: create array with 5 numbers. int[] values = { 1, 3, 5, 8, 10 }; // Part 2: take all non-even (odd) numbers. var result = values.TakeWhile(item => item % 2 != 0); foreach (int value in result) { Console.WriteLine(value); } } } Output 1 3 5
Skip and Take. We can combine Skip with Take (and SkipWhile, TakeWhile). In this example we indicate elements we want to avoid based on a numeric pattern.

Info: We call SkipWhile to skip some lower-value elements, and then call TakeWhile to take some until a limit is reached.

C# program that uses SkipWhile and TakeWhile using System; using System.Linq; class Program { static void Main() { int[] values = { 10, 20, 30, 40, 50, 60 }; // Use SkipWhile and TakeWhile together. var result = values.SkipWhile(v => v < 30).TakeWhile(v => v < 50); foreach (var value in result) { Console.WriteLine("RESULT: {0}", value); } } } Output RESULT: 30 RESULT: 40
A discussion. LINQ methods are often used together. Skip is useful when paired with Take. It allows you to avoid the first several elements in the sequence.
A summary. Take() gets elements from a collection. Also, we saw Reverse with Take, and how to use extension methods. TakeWhile accepts a predicate and is more powerful.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf