Mark odd or even for a list of int value with Select in CSharp

Description

The following code shows how to mark odd or even for a list of int value with Select.

Example


using System;//from   w  ww .ja va 2s .c  o m
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class MainClass {
    public static void Main() {
        var numbers =
           from n in Enumerable.Range(100, 50)
           select new { Number = n, OddEven = n % 2 == 1 ? "odd" : "even" };

        foreach (var n in numbers) {
            Console.WriteLine("The number {0} is {1}.", n.Number, n.OddEven);
        }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    LINQ »




Operator
Select
Where
OrderBy
Group
Join
Let
LINQ