using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
namespace foda
{
public static class LambdaHelper
{
///
/// 创建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, object propertyValue)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
ConstantExpression constant = Expression.Constant(propertyValue);//创建常数
return Expression.Lambda>(Expression.Equal(member, constant), parameter);
}
///
/// 创建lambda表达式:p=>p.propertyName != propertyValue
///
///
///
///
///
public static Expression> CreateNotEqual(string propertyName, string propertyValue)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
ConstantExpression constant = Expression.Constant(propertyValue);//创建常数
return Expression.Lambda>(Expression.NotEqual(member, constant), parameter);
}
///
/// 创建lambda表达式:p=>p.propertyName > propertyValue
///
///
///
///
///
public static Expression> CreateGreaterThan(string propertyName, string propertyValue)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
ConstantExpression constant = Expression.Constant(propertyValue);//创建常数
return Expression.Lambda>(Expression.GreaterThan(member, constant), parameter);
}
///
/// 创建lambda表达式:p=>p.propertyName < propertyValue
///
///
///
///
///
public static Expression> CreateLessThan(string propertyName, string propertyValue)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
ConstantExpression constant = Expression.Constant(propertyValue);//创建常数
return Expression.Lambda>(Expression.LessThan(member, constant), parameter);
}
///
/// 创建lambda表达式:p=>p.propertyName >= propertyValue
///
///
///
///
///
public static Expression> CreateGreaterThanOrEqual(string propertyName, string propertyValue)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
ConstantExpression constant = Expression.Constant(propertyValue);//创建常数
return Expression.Lambda>(Expression.GreaterThanOrEqual(member, constant), parameter);
}
///
/// 创建lambda表达式:p=>p.propertyName <= propertyValue
///
///
///
///
///
public static Expression> CreateLessThanOrEqual(string propertyName, string propertyValue)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
ConstantExpression constant = Expression.Constant(propertyValue);//创建常数
return Expression.Lambda>(Expression.LessThanOrEqual(member, constant), parameter);
}
//时间比较>=
public static Expression> CreateCompareToLater(string propertyName, object propertyValue)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
var methodInfo = typeof(DateTime).GetMethod("CompareTo", new Type[] { typeof(DateTime) });//
ConstantExpression constant = Expression.Constant(Convert.ToDateTime(propertyValue), typeof(DateTime));//创建常数
BinaryExpression siteNoExpression =
Expression.GreaterThanOrEqual(
Expression.Call(
member,
methodInfo,
constant
),
Expression.Constant(0, typeof(Int32)) //比较String.CompareTo的返回结果和0,来实现>=的效果
);
return Expression.Lambda>(siteNoExpression, parameter);
}
//时间比较<=
public static Expression> CreateCompareToEarlier(string propertyName, object propertyValue)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");//创建参数p
MemberExpression member = Expression.PropertyOrField(parameter, propertyName);
var methodInfo = typeof(DateTime).GetMethod("CompareTo", new Type[] { typeof(DateTime) });//
ConstantExpression constant = Expression.Constant(Convert.ToDateTime(propertyValue), typeof(DateTime));//创建常数
BinaryExpression siteNoExpression =
Expression.LessThanOrEqual(
Expression.Call(
member,
methodInfo,
constant
),
Expression.Constant(0, typeof(Int32)) //比较String.CompareTo的返回结果和0,来实现>=的效果
);
return Expression.Lambda>(siteNoExpression, 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);
}
public static Expression> Or(this Expression> expr1, Expression> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast());
return Expression.Lambda>
(Expression.Or(expr1.Body, invokedExpr), expr1.Parameters);
}
public static Expression> And(this Expression> expr1, Expression> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast());
return Expression.Lambda>
(Expression.And(expr1.Body, invokedExpr), expr1.Parameters);
}
}
}