Get the fourth number from a query in CSharp

Description

The following code shows how to get the fourth number from a query.

Example


  //  www . j a  va 2  s . c om
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass {
    public static void Main() {
        int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

        int fourthLowNum = (
            from n in numbers
            where n < 5
            select n)
            .ElementAt(3); // 3 because sequences use 0-based indexing

        Console.WriteLine("Fourth number < 5: {0}", fourthLowNum);
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ