How to use LINQ where clause to filter results in C#

Web developer can modify the where clause in LINQ statement to filter the results and to include only those that match a specific condition. The next code shows how to find clients who have a last name that starts with a specific letter:


var matches = from client in clients

where client.LastName.StartsWith(“P“)

select client;


Web developer can combine multiple conditional expressions with the and (&&) and or (||) operators, and he/she can use relational operators (such as <, <=, >, and >=) in conjunction with hard-coded values or other variables. For example, Web developer could create a query like this to filter out products greater than a certain price threshold:


var matches = from product in products

where product.UnitsInStock > 0 && product.UnitPrice > 75.00S

select product;