动态lambda 构建

时间:2022-03-10 16:41:16

var param = Expression.Parameter(typeof(T));
var datetime1 = Expression.Constant(dt1);
var datetime2 = Expression.Constant(dt2);
var body = Expression.And(
Expression.GreaterThanOrEqual(Expression.Property(param, propertyName), datetime1),
Expression.LessThanOrEqual(Expression.Property(param, propertyName), datetime2));
var e = Expression.Lambda<Func<T, bool>>(body, param);
var q = DataRepository.GetQueryable<T>().Where(e.Compile());