Restriction Operators (LINQ)
Posted by Rajinder
Last Updated: July 22, 2012

Where (Example 1)

This sample uses where to find all elements of an array less than 5.

public void Linq() 
    { 
        int[] numbers = { 5413986720 }; 
      
        var lowNums = 
            from n in numbers 
            where n < 5 
            select n; 
      
        Console.WriteLine("Numbers < 5:"); 
        foreach (var x in lowNums) 
        { 
            Console.WriteLine(x); 
        } 
    } 

Where (Example 2)

This sample uses where to find all products that are out of stock.

   public void Linq() 
    { 
        List<Product> products = GetProductList(); 
      
        var soldOutProducts = 
            from p in products 
            where p.UnitsInStock == 0 
            select p; 
      
        Console.WriteLine("Sold out products:"); 
        foreach (var product in soldOutProducts) 
        { 
            Console.WriteLine("{0} is sold out!", product.ProductName); 
        } 
    } 
Related Content