LINQ | Partition Operator | TakeWhile

28 May, 2019
In LINQ, partition operators are used for separating the given sequence into two portions without sorting the elements and return one of the portions. The Standard Query Operators supports 4 different types of partition operators:
SkipSkipWhileTakeTakeWhile
Skip
SkipWhile
Take
TakeWhile
The TakeWhile operator is used to return elements from the given sequence as long as it satisfies the given condition and when the condition satisfy it skips those elements which do not satisfy the given condition. This method is overloaded in two different ways:
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>): This method is used to return elements from the given sequence as long as a specified condition is true.
TakeWhile<TSource>(IEnumerable<TSource>, Func<TSource, Int32, Boolean>): This method is used to return elements from the given sequence as long as a specified condition is true. The index value of the elements are used in the logic of the predicate function.
Important Points:
It does not support query syntax in C# and VB.Net languages. But you can use the TakeWhile method to query variable or you can wrap your query in brackets and then the call TakeWhile method.
It support method syntax in both C# and VB.Net languages.
It present in both the Queryable and Enumerable class.
It is implemented by using deferred execution.
It will throw an ArgumentNullException if the source or the condition is null.
Example 1:
// C# program to illustrate the// concept of TakeWhile operatorusing System;using System.Linq;  class GFG {      static public void Main()    {          // Data source        int[] sequence = {45, 67, 89, 13, 56,                                76, 100, 90};          // Query to pick those elements        // which are less than 70        // Using TakeWhile method        var result = sequence.OrderBy(s => s).TakeWhile(s => s < 70);          Console.WriteLine("New Sequence: ");          // Display new sequence        foreach(var val in result)        {            Console.WriteLine(val);        }    }}
New Sequence: 
13
45
56
67

Example 2:
// C# program to get the names of the // employees whose salary is less // than 50000using System;using System.Linq;using System.Collections.Generic;  // Employee detailspublic class Employee {      public int emp_id    {        get;        set;    }    public string emp_name    {        get;        set;    }    public string emp_gender    {        get;        set;    }    public string emp_hire_date    {        get;        set;    }    public int emp_salary    {        get;        set;    }}  public class GFG {      // Main method    static public void Main()    {          List<Employee> emp = new List<Employee>() {              new Employee() {emp_id = 209, emp_name = "Anjita", emp_gender = "Female",                                    emp_hire_date = "12/3/2017", emp_salary = 20000},              new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female",                                    emp_hire_date = "22/4/2018", emp_salary = 30000},              new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male",                                  emp_hire_date = "3/5/2016", emp_salary = 40000},              new Employee() {emp_id = 212, emp_name = "Anu", emp_gender = "Female",                                  emp_hire_date = "4/8/2017", emp_salary = 80000},              new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male",                                emp_hire_date = "12/1/2016", emp_salary = 60000},              new Employee() {emp_id = 214, emp_name = "Anju", emp_gender = "Female",                                  emp_hire_date = "17/6/2015", emp_salary = 50000},        };          // Query to get the names of the        // employees whose salary is less        // than 50000 Using TakeWhile method        var res = emp.TakeWhile(e => e.emp_salary < 50000);          foreach(var val in res)        {            Console.WriteLine("Employee Name: {0}",                                     val.emp_name);        }    }}
Employee Name: Anjita
Employee Name: Soniya
Employee Name: Rohit

CSharp LINQ
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
C# Dictionary with examples
Introduction to .NET Framework
C# | Delegates
C# | Multiple inheritance using interfaces
Differences Between .NET Core and .NET Framework
C# | Method Overriding
C# | Data Types
C# | Constructors
C# | String.IndexOf( ) Method | Set - 1
C# | Class and Object