In this Video, I am going to discuss Where Filtering Operators in LINQ with examples. Please read our previous Video before proceeding to this Video where we discussed the SelectMany Projection operator in C# with some examples. At the end of this Video, you will understand the following concepts.

- What is Filtering
- What are the Filtering methods available in LINQ?
- The “Where” operator and its examples using both method and query syntax
- What is a Predicate?
What is Filtering?
Filtering is nothing but the process to get only those elements from a data source that satisfied the given condition. It is also possible to fetch the data from a data source with more than one condition as per our business requirement.
For example:
- Employees having a salary greater than 50000.
- Students Having Marks greater than 80% from a particular batch.
- Employees having experience more than 6 Years and the department is IT, etc.
What are the Filtering methods available in LINQ?
There are two methods provided by LINQ in C# which are used for filtering. They are as follows
- Where
- OfType
In this Video, I am going to discuss the “Where” operator in detail. In the next Video, I will discuss the OfType operator with some examples.
Where Filtering Operators in LINQ:
The standard query operator “where” comes under the Filtering Operators category in LINQ.
We need to use the where standard query operator in LINQ when we need to filter the data from a data source based on some condition(s) just like as we did in SQL using the where clause. So in simple words, we can say that it is used to filter the data from a data source based on some condition(s).
The “where” always expects at least one condition and we can specify the condition(s) using predicates. The conditions can be written using the following symbols
==, >=, <=, &&, ||, >, <, etc.
There are two overloaded versions of the “where” operator is available. They are as follows
As you can see in the above signatures, the methods are implemented as extension methods on IEnumerable<T> interface. The methods predicate as a parameter. So let us first discuss what a predicate is?
What is a Predicate?
A predicate is nothing but a function that is used to test each and every element for a given condition. Let us understand this with an example.
In the below example, the Lambda expression (num => num > 5) runs for each and every element present in the “intList” collection. Then it will check, whether the number is greater than 5 or not. If the number value is greater than 5, then a boolean value true is returned otherwise false.
When we execute the above program, it will give us the following output as expected.
When you hover the mouse over the WHERE extension method in the above example, then visual studio intelligence shows the following.
As you can see in the above image, the predicate (Func<int, bool> predicate) expects one input parameter of type integer and it returns a boolean value. The Func is a generic delegate that takes one or more input parameters and returns one out parameter. The last parameter is considered as the return value. The return type is mandatory but the input parameter is not.
If you are new to Generic Delegate, then I strongly recommended you to read the following Video where we discussed the Generic Delegates in C# with examples.
https://dotnettutorials.net/lesson/generic-delegates-csharp/
The lambda expression that we passed to the where extension method in the above example operates on integer data type and should return a boolean value else we will get a compile-time error.
So the following line of code from the above example
IEnumerable<int> filteredData = intList.Where(num => num > 5);
Can be rewritten as shown below
Func<int, bool> predicate = i => i > 5;
IEnumerable<int> filteredData = intList.Where(predicate);
It should give the same output as expected. You can also create a separate function as shown below which also work as expected.
Example2:
In the second overloaded version of the “where” extension method, the int parameter of the predicate function represents the index position of the source element.
Let us see an example to understand this.
Here we need to filter only the odd numbers i.e. the numbers which are not divisible by 2. Along with the numbers we also need to fetch the index position of the number. The index is 0 based.
Now run the application and you will see the odd numbers along with their index position as shown below.
Let’s rewrite the same example using query syntax.
Now run the application and it will also give the same output as method syntax output.
Complex Examples:
Let us see how to use where condition with complex type. We are going to use the following Employee class. So, create a class file with the name Employee.cs and then copy and paste the following code.
As we can see we created the Employee class with five properties i.e. ID, Name, Gender, Salary, and Technology. As you can see we have also created one method which will return the list of all employees which will be going to our data source.
Example3:
We need to fetch all the employees whose salary is greater than 50000.
Output:
Example4: Using multiple conditions
We need to fetch all the employee whose gender is Male and Salary is greater than 500000.
Output:
Example5:
Multiple conditions with the custom operation and projecting the data to an anonymous type:
Requirement:
All the employees whose salary is greater than or equal to 50000 and technology should not be null.
We need to fetch the following information to an anonymous type.
- Name as it is
- Gender as it is
- MonthlySalary = Salary / 12
The code is given below.
Output:
Example6: Fetching elements along with the Index position
Here we need to fetch all the employees whose Gender is Male and Salary is greater than 500000 along with their index position to an anonymous type.
Output: