lamda查询拼接条件
public class Student
{
public int Id { get; set; }
public int Age { get; set; }
public string Name { get; set; }
}
List<Student> persons = new List<Student>();
persons.Add(new Student { Id = 1, Age = 25, Name = "张三" });
persons.Add(new Student { Id = 2, Age = 30, Name = "李四" });
persons.Add(new Student { Id = 3, Age = 18, Name = "柳白" });
persons.Add(new Student { Id = 4, Age = 22, Name = "慕容" });
Func<Student, bool> where1 = x => x.Age > 20;
Func<Student, bool> where2 = x => x.Id > 1;
var result = persons.Where(where1).Where(where2);
Console.Write("结果: ");
string printString = string.Empty;
foreach (var val in result)
{
if (string.IsNullOrEmpty(printString))
{
printString = val.Name;
}
else
{
printString +=" ,"+ val.Name;
}
}
Console.Write(printString);
Console.ReadLine();
上面的代码实现了,但执行2次查询,本来想通过多播委托实现
Func<Student, bool> where1 = x => x.Age > 20;
where1 += x => x.Id > 1;
var result = persons.Where(where1);
但结果第一个条件不能查询!
我想通过拼接,就像条件通过外部参数不同而改变,像自由拼接SQL语句一样,最后变成一条完整的语句执行,不通过多次执行。所以不想这样实现
Func<Student, bool> where1 = x => x.Age > 20 && x.Id > 1;
var result = persons.Where(where1);
有其他的实现吗?谢谢!
--------------------编程问答-------------------- http://www.cnblogs.com/wengyuli/archive/2010/11/06/1870614.html
补充:.NET技术 , C#