Use LINQ statement to query an int array in CSharp

Description

The following code shows how to use LINQ statement to query an int array.

Example


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

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

        var lowNums =
            from n in numbers
            where n < 5
            select n;

        Console.WriteLine("Numbers < 5:");
        foreach (var x in lowNums) {
            Console.WriteLine(x);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ