由于业务需要,在网上搜索了相关内容,整理了如下帮助类:
///
/// 动态生成比较lamda表达式帮助类
///
public class LamdaHelper
{
///
/// 创建lambda表达式:p=>true
///
/// 对象名称(类名)
///
public static Expression> True()
{
return p => true;
}
///
/// 创建lambda表达式:p=>false
///
/// 对象名称(类名)
///
public static Expression> False()
{
return p => false;
}
///
/// 创建lambda表达式:p=>p.propertyName
///
/// 对象名称(类名)
/// 参数类型
/// 字段名称(数据库中字段名称)
///
public static Expression> GetOrderExpression(string propertyName)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");
return Expression.Lambda>(Expression.Property(parameter, propertyName), parameter);
}
///
/// 创建lambda表达式:p=>p.propertyName == propertyValue
///
/// 对象名称(类名)
/// 参数类型
/// 字段名称(数据库中字段名称)
/// 数据值
///
public static Expression> CreateEqual(string propertyName, S propertyValue)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
ConstantExpression constant = Expression.Constant(propertyValue, typeof(S));//创建常数
return Expression.Lambda>(Expression.Equal(member, constant), parameter);
}
///
/// 创建lambda表达式:p=>p.propertyName != propertyValue
///
/// 对象名称(类名)
/// 参数类型
/// 字段名称(数据库中字段名称)
/// 数据值
///
public static Expression> CreateNotEqual(string propertyName, S propertyValue)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
ConstantExpression constant = Expression.Constant(propertyValue, typeof(S));//创建常数
return Expression.Lambda>(Expression.NotEqual(member, constant), parameter);
}
///
/// 创建lambda表达式:p=>p.propertyName > propertyValue
///
/// 对象名称(类名)
/// 参数类型
/// 字段名称(数据库中字段名称)
/// 数据值
///
public static Expression> CreateGreaterThan(string propertyName, S propertyValue)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
ConstantExpression constant = Expression.Constant(propertyValue, typeof(S));//创建常数
return Expression.Lambda>(Expression.GreaterThan(member, constant), parameter);
}
///
/// 创建lambda表达式:p=> propertyValue > p.propertyName
///
/// 对象名称(类名)
/// 参数类型
/// 字段名称(数据库中字段名称)
/// 数据值
///
public static Expression> CreateLessThan(string propertyName, S propertyValue)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
ConstantExpression constant = Expression.Constant(propertyValue, typeof(S));//创建常数
return Expression.Lambda>(Expression.LessThan(member, constant), parameter);
}
///
/// 创建lambda表达式:p=>p.propertyName >= propertyValue
///
/// 对象名称(类名)
/// 参数类型
/// 字段名称(数据库中字段名称)
/// 数据值
///
public static Expression> CreateGreaterThanOrEqual(string propertyName, S propertyValue)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
ConstantExpression constant = Expression.Constant(propertyValue, typeof(S));//创建常数
return Expression.Lambda>(Expression.GreaterThanOrEqual(member, constant), parameter);
}
///
/// 创建lambda表达式:p=>propertyValue >= p.propertyName
///
/// 对象名称(类名)
/// 参数类型
/// 字段名称(数据库中字段名称)
/// 数据值
///
public static Expression> CreateLessThanOrEqual(string propertyName, S propertyValue)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
ConstantExpression constant = Expression.Constant(propertyValue, typeof(S));//创建常数
return Expression.Lambda>(Expression.LessThanOrEqual(member, constant), parameter);
}
///
/// 创建lambda表达式:p=>p.propertyName.Contains(propertyValue)
///
/// 对象名称(类名)
/// 字段名称(数据库中字段名称)
/// 数据值
///
public static Expression> GetContains(string propertyName, string propertyValue)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");
MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
ConstantExpression constant = Expression.Constant(propertyValue, typeof(string));
return Expression.Lambda>(Expression.Call(member, method, constant), parameter);
}
///
/// 创建lambda表达式:!(p=>p.propertyName.Contains(propertyValue))
///
/// 对象名称(类名)
/// 字段名称(数据库中字段名称)
/// 数据值
///
public static Expression> GetNotContains(string propertyName, string propertyValue)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");
MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
ConstantExpression constant = Expression.Constant(propertyValue, typeof(string));
return Expression.Lambda>(Expression.Not(Expression.Call(member, method, constant)), parameter);
}
}