Use Index in Where clause in CSharp

Description

The following code shows how to use Index in Where clause.

Example


using System;/*from w  ww .  j a v a  2  s.  c  om*/
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass {
    public static void Main() {

        string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

        var shortDigits = digits.Where((digit, index) => digit.Length < index);

        foreach (var d in shortDigits) {
            Console.WriteLine("The word {0} is shorter than its value.", d);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ