注塑喷涂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

220 lines
11 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace MESClassLibrary
{
public static class LambdaHelper
{
/// <summary>
/// 创建lambda表达式:p=>true
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Expression<Func<T, bool>> True<T>()
{
return p => true;
}
/// <summary>
/// 创建lambda表达式:p=>false
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static Expression<Func<T, bool>> False<T>()
{
return p => false;
}
/// <summary>
/// 创建lambda表达式:p=>p.propertyName
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TKey"></typeparam>
/// <param name="sort"></param>
/// <returns></returns>
public static Expression<Func<T, TKey>> GetOrderExpression<T, TKey>(string propertyName)
{
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");
return Expression.Lambda<Func<T, TKey>>(Expression.Property(parameter, propertyName), parameter);
}
/// <summary>
/// 创建lambda表达式:p=>p.propertyName == propertyValue
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="column"></param>
/// <param name="value"></param>
/// <returns></returns>
public static Expression<Func<T, bool>> CreateEqual<T>(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<Func<T, bool>>(Expression.Equal(member, constant), parameter);
}
/// <summary>
/// 创建lambda表达式:p=>p.propertyName != propertyValue
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="propertyName"></param>
/// <param name="propertyValue"></param>
/// <returns></returns>
public static Expression<Func<T, bool>> CreateNotEqual<T>(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<Func<T, bool>>(Expression.NotEqual(member, constant), parameter);
}
/// <summary>
/// 创建lambda表达式:p=>p.propertyName > propertyValue
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="column"></param>
/// <param name="value"></param>
/// <returns></returns>
public static Expression<Func<T, bool>> CreateGreaterThan<T>(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<Func<T, bool>>(Expression.GreaterThan(member, constant), parameter);
}
/// <summary>
/// 创建lambda表达式:p=>p.propertyName < propertyValue
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="column"></param>
/// <param name="value"></param>
/// <returns></returns>
public static Expression<Func<T, bool>> CreateLessThan<T>(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<Func<T, bool>>(Expression.LessThan(member, constant), parameter);
}
/// <summary>
/// 创建lambda表达式:p=>p.propertyName >= propertyValue
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="column"></param>
/// <param name="value"></param>
/// <returns></returns>
public static Expression<Func<T, bool>> CreateGreaterThanOrEqual<T>(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<Func<T, bool>>(Expression.GreaterThanOrEqual(member, constant), parameter);
}
/// <summary>
/// 创建lambda表达式:p=>p.propertyName <= propertyValue
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="column"></param>
/// <param name="value"></param>
/// <returns></returns>
public static Expression<Func<T, bool>> CreateLessThanOrEqual<T>(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<Func<T, bool>>(Expression.LessThanOrEqual(member, constant), parameter);
}
//时间比较>=
public static Expression<Func<T, bool>> CreateCompareToLater<T>(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<Func<T, bool>>(siteNoExpression, parameter);
}
//时间比较<=
public static Expression<Func<T, bool>> CreateCompareToEarlier<T>(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<Func<T, bool>>(siteNoExpression, parameter);
}
/// <summary>
/// 创建lambda表达式:p=>p.propertyName.Contains(propertyValue)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="column"></param>
/// <param name="value"></param>
/// <returns></returns>
public static Expression<Func<T, bool>> GetContains<T>(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<Func<T, bool>>(Expression.Call(member, method, constant), parameter);
}
/// <summary>
/// 创建lambda表达式:!(p=>p.propertyName.Contains(propertyValue))
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="column"></param>
/// <param name="value"></param>
/// <returns></returns>
public static Expression<Func<T, bool>> GetNotContains<T>(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<Func<T, bool>>(Expression.Not(Expression.Call(member, method, constant)), parameter);
}
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>
(Expression.Or(expr1.Body, invokedExpr), expr1.Parameters);
}
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>
(Expression.And(expr1.Body, invokedExpr), expr1.Parameters);
}
}
}