在Lambda表达式中使用多个条件进行筛选,可以通过逻辑运算符将多个条件组合起来。以下是一些常见的情况及示例:
一、使用 &&
(与)运算符组合条件
当需要筛选出同时满足多个条件的元素时,可以使用 &&
运算符将这些条件连接起来。例如,假设有一个包含学生对象的列表,每个学生对象有 Name
(姓名)、Age
(年龄)和 Score
(成绩)等属性,现在要筛选出年龄在18到22岁之间且成绩大于80分的学生,示例代码如下:
class Student
{
public string Name;
public int Age;
public int Score;
public Student(string name, int Age, int score)
{
Name = name;
this.Age = Age;
Score = score;
}
}
List<Student> students = new List<Student>()
{
new Student("Alice", 19, 85),
new Student("Bob", 20, 78),
new Student("Charlie", 23, 82),
new Student("David", 17, 90),
new Student("Eve", 21, 88)
};
var selectedStudents = students.Where(s => s.Age >= 18 && s.Age <= 22 && s.Score > 80).ToList();
foreach (Student student in selectedStudents)
{
Console.WriteLine($"{student.Name}, {student.Age}, {student.Score}");
}
在上述代码中,Where
方法用于筛选元素,其参数是一个Lambda表达式 s => s.Age >= 18 && s.Age <= 22 && s.Score > 80
。这里的 s
代表列表中的每个学生对象,通过 &&
运算符将三个条件连接起来,表示只有同时满足年龄在18到22岁之间以及成绩大于80分这两个条件的学生才会被筛选出来。
二、使用 ||
(或)运算符组合条件
如果要筛选出满足其中任意一个或多个条件的元素,可以使用 ||
运算符。例如,假设有一个包含商品对象的列表,每个商品对象有 Name
(名称)、Price
(价格)和 Category
(类别)等属性,现在要筛选出价格小于10元或者类别为“电子产品”的商品,示例代码如下:
class Product
{
public string Name;
public decimal Price;
public string Category;
public Product(string name, decimal price, string category)
{
Name = name;
Price = price;
Category = category;
}
}
List<Product> products = new List<Product>()
{
new Product("Apple", 8, "Fruits"),
new Product("Laptop", 5000, "Electronics"),
new Product("Banana", 5, "Fruits"),
new Product("Mouse", 20, "Electronics"),
new Product("Orange", 12, "Fruits")
};
var selectedProducts = products.Where(p => p.Price < 10 || p.Category == "Electronics").ToList();
foreach (Product product in selectedProducts)
{
Console.WriteLine($"{product.Name}, {product.Price}, {product.Category}");
}
在上述代码中,Where
方法的Lambda表达式参数 p => p.Price < 10 || p.Category == "Electronics"
中,p
代表列表中的每个商品对象,通过 ||
运算符将两个条件连接起来,表示只要满足价格小于10元或者类别为“电子产品”其中一个条件的商品就会被筛选出来。
三、混合使用逻辑运算符
有时候还需要混合使用 &&
和 ||
运算符来实现更复杂的筛选条件。例如,假设有一个包含员工对象的列表,每个员工对象有 Name
(姓名)、Age
(年龄)、Department
(部门)和 Salary
(工资)等属性,现在要筛选出年龄在25到35岁之间且(工资大于5000元或者部门为“研发部”)的员工,示例代码如下:
class Employee
{
public string Name;
public int Age;
public string Department;
public decimal Salary;
public Employee(string name, int age, string department, decimal salary)
{
Name = name;
Age = age;
Department = department;
Salary = salary;
}
}
List<Employee> employees = new List<Employee>()
{
new Employee("Alice", 23, "Marketing", 4000),
new Employee("Bob", 28, "R&D", 6000),
new Employee("Charlie", 32, "HR", 5500),
new Employee("David", 38, "R&D", 7000),
new Employee("Eve", 20, "Marketing", 3500)
};
var selectedEmployees = employees.Where(e => e.Age >= 25 && e.Age <= 35 && (e.Salary > 5000 || e.Department == "R&D")).ToList();
foreach (Employee employee in selectedEmployees)
{
Console.WriteLine($"{employee.Name}, {employee.Age}, {employee.Department}, {employee.Salary}");
}
在上述代码中,Where
方法的Lambda表达式参数 e => e.Age >= 25 && e.Age <= 35 && (e.Salary > 5000 || e.Department == "R&D")
中,e
代表列表中的每个员工对象。首先通过 &&
运算符确保年龄在25到35岁之间这个条件,然后通过括号将工资大于5000元或者部门为“研发部”这两个条件括起来,再用 &&
运算符与前面的年龄条件进行连接,表示只有同时满足整体的这些条件的员工才会被筛选出来。
通过合理运用逻辑运算符,就可以在Lambda表达式中轻松地组合多个条件进行筛选,以满足各种复杂的业务需求。