学习笔记: Expression表达式目录树详解和扩展封装
1. 表达式链接扩展封装,ORM常用 And Or
/// <summary>
/// 表达式访问者
/// </summary>
public class ExpressionVisitorTest
{public static void Show()
{
#region 表达式链接
{
Expression<Func<People, bool>> lambda1 = x => x.Age > 5;
Expression<Func<People, bool>> lambda2 = x => x.Id > 5;
Expression<Func<People, bool>> lambda3 = lambda1.And(lambda2);
Expression<Func<People, bool>> lambda4 = lambda1.Or(lambda2);
Expression<Func<People, bool>> lambda5 = lambda1.Not();
Do1(lambda3);
Do1(lambda4);
Do1(lambda5);
}
#endregion
}
#region PrivateMethod
private static void Do1(Func<People, bool> func)
{
List<People> people = new List<People>();
people.Where(func);
}
private static void Do1(Expression<Func<People, bool>> func)
{
List<People> people = new List<People>()
{
new People(){Id=4,Name="123",Age=4},
new People(){Id=5,Name="234",Age=5},
new People(){Id=6,Name="345",Age=6},
};List<People> peopleList = people.Where(func.Compile()).ToList();
}private static IQueryable<People> GetQueryable(Expression<Func<People, bool>> func)
{
List<People> people = new List<People>()
{
new People(){Id=4,Name="123",Age=4},
new People(){Id=5,Name="234",Age=5},
new People(){Id=6,Name="345",Age=6},
};return people.AsQueryable<People>().Where(func);
}
#endregion
}