LINQ Keywords: where
Home

LINQ Keywords: where

     

Where is the Condition

The where keyword is used to apply a criterion to an expression. The formula to follow is:

var SubListName = from ValueHolder in List where Condition select ValueHolder;

The criterion is created as a Boolean expression. Here is an example:

using System;
using System.Linq;
using System.Collections.Generic;

public class Exercise
{
    public static int Main()
    {
        var numbers = new List<int>();

        numbers.Add(12);
        numbers.Add(45);
        numbers.Add(38);
        numbers.Add(5);
        numbers.Add(128);
        numbers.Add(525);
        numbers.Add(2448);
        numbers.Add(39);
        numbers.Add(632);
        numbers.Add(207);

        var number = from n
                     in numbers
                     where n == 525
                     select n;

        foreach (var member in number)
            Console.WriteLine(member.ToString());

        Console.WriteLine();
        return 0;
    }
}

This would produce:

LINQ Where

Here is another example:

using System;
using System.Linq;
using System.Collections.Generic;

public class Exercise
{
    public static int Main()
    {
        var numbers = new List<int>();

        numbers.Add(12);
        numbers.Add(45);
        numbers.Add(38);
        numbers.Add(5);
        numbers.Add(128);
        numbers.Add(525);
        numbers.Add(2448);
        numbers.Add(39);
        numbers.Add(632);
        numbers.Add(207);

        var number = from n
                     in numbers
                     where (n % 5) == 0
                     select n;

        foreach (var member in number)
            Console.WriteLine(member.ToString());

        Console.WriteLine();
        return 0;
    }
}

This would produce:

LINQ Where

 

Home Copyright © 2008-2011 FunctionX