LINQ where Clause

You can filter the results of the query by using the where clause in a query expression. Following the where keyword is the condition (or conditions) that must be met by an item to be included in the results. The great factor with LINQ is that you can use the obtainable ways in .NET Framework to supplement your condition. This lesson can show you some examples on victimisation the where clause to filter queried information.

You can use relational operators to compare a value or property of an item into another value. For example, suppose we want to retrieve numbers which are greater than 5 from an array of numbers, we can do so using the following LINQ query.

int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

var greaterThanFive = from number in numbers
                      where number > 5
                      select number;

The where clause states that the value of a number must be greater than 5 for it to be selected and included. We can even use logical operators for more complex conditions such as the following:

var sixToTen = from number in numbers
               where number > 5 && number <= 10
               select number;

The query above only retrieves values greater than 5 and less than or equal 10, that is, values 6 to 10. If you have a collection of objects with several properties, you can also test those properties if they met the required condition.

List<Person> people = GetPersonList();

var smiths = from person in people
             where person.LastName == "Smith"
             select person;

The query retrieves every person whose LastName is Smith. We can use .NET methods in our condition. For instance, suppose we want to retrieve all the person whose last name starts with the letter ‘R’.

var startsWithR = from person in people
 where person.LastName.StartsWith("R")
 select person;

We used the StartsWith() method of the String class which returns  true if a particular string starts with a specified string argument of the method.

Alternatively, we can use the Where() extension method from the System.Linq namespace. You can then pass a lambda expression that has one parameter representing every item from the data source and its body containing the boolean expression for the condition. The method returns the collection of objects that pass the required condition.

var greaterThanFive = numbers.Where ( number => number > 5 );

The lambda expression’s body is a single condition which tests the value of number if it is greater than 5. The following is another example which retrieves persons whose last name starts with ‘R’.

var startsWithR = people.Where( person => person.LastName.StartsWith("R") );

You can use another overloaded version of the method which accepts a lambda expression having two parameters. The first parameter represents every object from the collection, and the second parameter represents the index of that item in the collection. The following call to Where() method returns numbers whose index in its collection is even.

var evenIndices = numbers.Where( (number, index) => index % 2 == 0 );

You can use the Where() method when you simply want to filter a collection using a specified condition.