Use value and index with TakeWhile in CSharp

Description

The following code shows how to use value and index with TakeWhile.

Example


   /*ww w . j  av  a  2 s  . c o m*/

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
    public static void Main() {

        string[] presidents = {"ant", "Hard", "Harrison", "Hayes", "Hoover", "Jack"};

        IEnumerable<string> items = presidents
         .TakeWhile((s, i) => s.Length < 10 && i < 5);

        foreach (string item in items)
            Console.WriteLine(item);

    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ