One of the fundamental methods provided by LINQ is the Where filtering method, which allows developers to filter data based on specified conditions. In this article, we will discuss on LINQ’s Where method and explore its various applications in C#.

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 requirements. For example:

  1. Employees having a salary greater than 50000.
  2. Students Having Marks greater than 80% from a particular batch.
  3. Employees having experience of more than 6 Years and the department is IT, etc.

The “Where” Method always expects at least one condition and we can specify the condition(s) using predicates. The conditions can be written using the ==, >=, <=, &&, ||, >, <, etc. symbols. 

What is a Predicate in C#?

A Predicate is nothing but a function or technically you can say a delegate that is used to test each and every element for a given condition. 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, then a boolean value true is returned otherwise false. In the below example, I am showing both Method and Query Syntax.

Second Overloaded Version of the Where Method in LINQ

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.

Rewrite the same example using Query Syntax

Now run the application and it will also give the same output as the method syntax output as shown in the below image.

Complex Example for LINQ Where Extension

Let us see how to use the LINQ Where Extension Method with Complex Data Type using C#. 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 into it. As we can see. we created the following Employee class with five properties i.e. ID, Name, Gender, Salary, and Technology. Here, we have also created one method which will return the list of all employees which will be going to our data source.

Now, we need to fetch all the employees whose salary is greater than 50000. For this, we need to use the LINQ Where Extension Method and we need to specify the condition as emp => emp.Salary > 50000 using Method Syntax and employee.Salary > 50000 using Query Syntax which is shown in the below example.

Multiple Conditions using LINQ Where Method

We need to fetch all the employees whose gender is Male and also whose Salary is greater than 500000. So, here we have two conditions. The first condition is Gender = Male and the second condition is Salary > 500000. If we have more than one condition, then we need to use && (AND) or || (OR) logical Operators based on our requirement. Here, we are going to use the AND (&&). That means if both the conditions satisfy then only return the data.

Complex Example to Understand LINQ Where Method

Let us see a more complex example to understand the LINQ Where Extension Method. Now, we are going to provide Multiple conditions with the custom operations and project the data to an anonymous type. The following is our business requirement.

We need to fetch all the employees whose salary is greater than or equal to 50000 and technology should not be null. And we need to project the following information to an anonymous type.

  1. Name as it is
  2. Gender as it is
  3. MonthlySalary = Salary / 12

The Complete Example Code is Given Below. In the below example, I am showing both the Method and Query syntax with the Where Extension Method to achieve the above requirement.

Index position using Where Method in C#

Now, we need to fetch all the employees whose Gender is Male and whose Salary is greater than 500000 along with their index position to an anonymous type.

The Where method in LINQ is a powerful tool for filtering data in C#. It allows developers to easily define and apply conditions on collections, resulting in concise and readable code. By leveraging the expressive syntax of lambda expressions, C# programmers can filter and extract precisely the data they need from a variety of data sources. Understanding the Where method and

Leave a Comment