Saturday, July 26, 2008

LINQ - Restriction Operator 'where'

The .NET Framework 3.5 version extends the .NET Framework 2.0 and 3.0 versions. One of the new features is the Language-Integrated Query (LINQ) and this extends powerful query capabilities to the C# language syntax (and VB as well) in the form of standard easily-learned query patterns.

A very simple program that displays the odd digits in LINQ:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace TestLinQ
{
    class Program
    {
        static void Main(string[] args)
        {
           // declare an array of integers
           int[] digits = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 
           // find all odd digits
           var oddDigits = from n in digits where n % 2 == 1 select n;

           // display all odd digits
           foreach(var x in oddDigits)
           {
              Console.WriteLine(x);
           }
        }
    }
}
 
This is indeed my first LINQ experience ;-), more precisely with the restriction operation 'where' ...

No comments: