Restriction Operator in LINQ

Restriction Operatorswhere is the restriction operator. It applies filter criteria on the sequence. The values of the sequence are filtered based on a supplied predicate. The where operator does not initiate the execution of the query. The query is executed when enumeration over the object is initiated, at which point the filter is applied.
This sample prints each element of an input integer array whose value is less than 5. The sample uses a query expression to create a new sequence of integers and then iterates over each element in the sequence, printing its value.

Where – Simple 1

public void Linq1() 
{ 
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 
var lowNums = from n in numbers where n < 5 select n; 
Console.WriteLine("Numbers < 5:");
 foreach (var x in lowNums) 
{ 
Console.WriteLine(x); 
} 
}

Where - Simple 2
This sample prints a list of all products that are out of stock. It selects each item from the product list where the number of units in stock equals zero.

public void Linq2() 
{ 
List 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); 
}
 }

Where - Simple 3
This sample lists all expensive items in stock. It uses a query expression that selects items from the product list where the number of items in stock is non-zero and the item's unit price is greater than 3.00

public void Linq3()
 { 
List products = GetProductList(); 
var expensiveInStockProducts = from p in products where p.UnitsInStock > 0 && p.UnitPrice > 3.00M select p; 
Console.WriteLine("In-stock products that cost more than 3.00:"); 
foreach (var product in expensiveInStockProducts) 
{ 
Console.WriteLine("{0} is in stock and costs more than 3.00.", product.ProductName);
 } 
}

Where - Drilldown
This sample prints a list of customers from the state of Washington along with their orders. A sequence of customers is created by selecting customers where the region is 'WA'. The sample uses doubly nested foreach statements to print the order numbers for each customer in the sequence.

public void Linq4() 
{ 
List customers = GetCustomerList();
 var waCustomers = from c in customers where c.Region == "WA" select c; 
Console.WriteLine("Customers from Washington and their orders:"); 
foreach (var customer in waCustomers) 
{ 
Console.WriteLine("Customer {0}: {1}", customer.CustomerID, customer.CompanyName); 
foreach (var order in customer.Orders) 
{ 
Console.WriteLine(" Order {0}: {1}", order.OrderID, order.OrderDate);
 }
 }
 }
SiteLock