
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
C# Linq TakeWhile() Method
Get elements as long as the condition is true in a sequence using the TakeWhile() method.
The following is our list with strings.
IList<string> str = new List<string>(){ "Car", "Bus", "Truck", "Airplane"};
Now, let’s say we need the strings whose length is less than 4. For that, use Lambda Expressions and add it as a condition in the TakeWhile() method.
str.TakeWhile(a => a.Length < 4);
Here is the example that displays elements until the condition is trie.
Example
using System; using System.Linq; using System.Collections.Generic; public class Demo { public static void Main() { IList<string> str = new List<string>(){ "Car", "Bus", "Truck", "Airplane"}; var res = str.TakeWhile(a => a.Length < 4); foreach(var arr in res) Console.WriteLine(arr); } }
Output
Car Bus
- Related Articles
- TakeWhile method in C# ()
- C# Linq Distinct() Method
- C# Linq Where Method
- C# Linq Intersect Method
- C# Linq FirstorDefault Method
- C# Linq Reverse Method
- C# Linq Select Method
- C# Linq SelectMany Method
- C# Linq Skip() Method
- C# Linq SkipLast Method
- C# Linq Sum() Method
- C# Linq ThenBy Method
- C# Linq LastorDefault Method
- C# Linq Zip Method
- C# Linq Contains Method

Advertisements