LINQ Query : Filtering / Where

Probably the most common query operation is to apply a filter in the form of a Boolean expression. The filter causes the query to return only those elements for which the expression is true. The result is produced by using the where clause. The filter in effect specifies which elements to exclude from the source sequence. In the following example, only those customers who have an address in London are returned.

var queryLondonCustomers = from cust in customers
where cust.City == "London"
select cust;

You can use the familiar C# logical AND and OR operators to apply as many filter expressions as necessary in the where clause. For example, to return only customers from "London" AND whose name is "Devon" you would write the following code:


where cust.City=="London" && cust.Name == "Devon"

To return customers from London or Paris, you would write the following code:


where cust.City == "London" || cust.City == "Paris"