Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Multiple Where clause in C# Linq
In C# LINQ, you can apply multiple where clauses to filter collections based on different conditions. Each additional where clause further narrows down the results, creating a logical AND relationship between the conditions.
Syntax
There are two main approaches to using multiple where clauses −
Query Syntax with Multiple Where Clauses:
var result = from item in collection
where condition1
where condition2
select item;
Method Syntax with Chained Where Methods:
var result = collection.Where(condition1).Where(condition2);
Single Where Clause with Combined Conditions:
var result = from item in collection
where condition1 && condition2
select item;
Using Multiple Where Clauses in Query Syntax
Multiple where clauses in LINQ query syntax are automatically combined with logical AND operations −
using System;
using System.Linq;
using System.Collections.Generic;
public class Employee {
public int EmpID { get; set; }
public string EmpName { get; set; }
public int EmpMarks { get; set; }
public int Rank { get; set; }
}
public class Demo {
public static void Main() {
IList<Employee> employees = new List<Employee>() {
new Employee() { EmpID = 1, EmpName = "Tom", EmpMarks = 90, Rank = 8},
new Employee() { EmpID = 2, EmpName = "Anne", EmpMarks = 60, Rank = 21 },
new Employee() { EmpID = 3, EmpName = "Jack", EmpMarks = 76, Rank = 18 },
new Employee() { EmpID = 4, EmpName = "Amy", EmpMarks = 67, Rank = 20},
};
var result = from e in employees
where e.Rank > 5
where e.Rank < 20
where e.EmpMarks > 65
select e;
Console.WriteLine("Employees with Rank between 5-20 and Marks > 65:");
foreach (var emp in result) {
Console.WriteLine($"Name: {emp.EmpName}, Marks: {emp.EmpMarks}, Rank: {emp.Rank}");
}
}
}
The output of the above code is −
Employees with Rank between 5-20 and Marks > 65: Name: Tom, Marks: 90, Rank: 8 Name: Jack, Marks: 76, Rank: 18
Using Chained Where Methods
You can achieve the same result using method syntax with chained Where calls −
using System;
using System.Linq;
using System.Collections.Generic;
public class Employee {
public int EmpID { get; set; }
public string EmpName { get; set; }
public int EmpMarks { get; set; }
public int Rank { get; set; }
}
public class Demo {
public static void Main() {
IList<Employee> employees = new List<Employee>() {
new Employee() { EmpID = 1, EmpName = "Tom", EmpMarks = 90, Rank = 8},
new Employee() { EmpID = 2, EmpName = "Anne", EmpMarks = 60, Rank = 21 },
new Employee() { EmpID = 3, EmpName = "Jack", EmpMarks = 76, Rank = 18 },
new Employee() { EmpID = 4, EmpName = "Amy", EmpMarks = 67, Rank = 20},
};
var result = employees
.Where(e => e.Rank > 5)
.Where(e => e.Rank < 20)
.Where(e => e.EmpMarks > 65);
Console.WriteLine("Using Method Syntax:");
foreach (var emp in result) {
Console.WriteLine($"Name: {emp.EmpName}, Marks: {emp.EmpMarks}, Rank: {emp.Rank}");
}
}
}
The output of the above code is −
Using Method Syntax: Name: Tom, Marks: 90, Rank: 8 Name: Jack, Marks: 76, Rank: 18
Comparison of Approaches
| Approach | Syntax | Performance | Readability |
|---|---|---|---|
| Multiple Where Clauses | where condition1 where condition2 | Identical performance | Clear separation of conditions |
| Single Combined Where | where condition1 && condition2 | Identical performance | Compact but can become complex |
| Chained Where Methods | .Where(c1).Where(c2) | Identical performance | Functional programming style |
Complex Filtering Example
Here's an example combining different data types and conditions −
using System;
using System.Linq;
using System.Collections.Generic;
public class Product {
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
public bool InStock { get; set; }
}
public class Demo {
public static void Main() {
var products = new List<Product> {
new Product { Id = 1, Name = "Laptop", Price = 800m, Category = "Electronics", InStock = true },
new Product { Id = 2, Name = "Mouse", Price = 25m, Category = "Electronics", InStock = false },
new Product { Id = 3, Name = "Desk", Price = 150m, Category = "Furniture", InStock = true },
new Product { Id = 4, Name = "Phone", Price = 600m, Category = "Electronics", InStock = true }
};
var filteredProducts = from p in products
where p.Category == "Electronics"
where p.Price > 50
where p.InStock == true
select p;
Console.WriteLine("Electronics products > $50 that are in stock:");
foreach (var product in filteredProducts) {
Console.WriteLine($"{product.Name}: ${product.Price}");
}
}
}
The output of the above code is −
Electronics products > $50 that are in stock: Laptop: $800 Phone: $600
Conclusion
Multiple where clauses in C# LINQ provide a clean way to apply complex filtering logic to collections. Whether using query syntax with multiple where clauses or method syntax with chained Where calls, the performance is identical, allowing you to choose based on readability preferences.
