public static class ExpressionHelp
{
private static Expression<T> Combine<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
{
MyExpressionVisitor visitor = new MyExpressionVisitor(first.Parameters[0]);
Expression bodyone = visitor.Visit(first.Body);
Expression bodytwo = visitor.Visit(second.Body);
return Expression.Lambda<T>(merge(bodyone, bodytwo), first.Parameters[0]);
}
/// <summary>
/// 条件合并,以and 的逻辑合并两个表达式
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="first"></param>
/// <param name="second"></param>
/// <returns></returns>
public static Expression<Func<T, bool>> ExpressionAnd<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Combine(second, Expression.And);
}
/// <summary>
/// 条件合并,以or 的逻辑合并两个表达式
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="first"></param>
/// <param name="second"></param>
/// <returns></returns>
public static Expression<Func<T, bool>> ExpressionOr<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Combine(second, Expression.Or);
}

/// <summary>
/// 查询语句,查询字段在 keywords中, 值为T 实体中与其对应的关键字包含值的数据。
/// </summary>
/// <typeparam name="T">实体类型 </typeparam>
/// <param name="item">实体</param>
/// <param name="Keywords">判断关键字</param>
/// <returns></returns>
public static Expression<Func<T, bool>> EntityEquel_Expression<T>(T item, List<string> Keywords)
{
Type entityType = typeof(T);
Expression<Func<T, bool>> expression = null;
ParameterExpression paramExpr = Expression.Parameter(typeof(T), "e");
foreach (var keyWord in Keywords)
{
Expression<Func<T, bool>> lambda;
string field = keyWord;//关键字
PropertyInfo proInfo = entityType.GetProperty(field);
var keyValue = proInfo.GetValue(item);
MemberExpression mExpr = Expression.Property(paramExpr, field);  
ConstantExpression cExpr = Expression.Constant(keyValue, proInfo.PropertyType);  
BinaryExpression bExpr = Expression.Equal(mExpr, cExpr);   
Expression whereExpr = bExpr;
lambda = Expression.Lambda<Func<T, bool>>(whereExpr, paramExpr);
if (expression != null)
{
expression = expression.ExpressionAnd(lambda);
}
else
{
expression = lambda;
}
}
return expression;
}
/// <summary>
/// 包含查询关键字 是否包含在list中 相当于sql中的 select key in (设定的值)
/// </summary>
/// <typeparam name="T">实体类</typeparam>
/// <param name="KeyValues"> 数据判断 字段 ,string 为 实体内的某个字符,list<string> 为包含该字符的条件 </param>
/// <returns></returns>
public static Expression<Func<T, bool>> In_Expression<T>(Dictionary<string, List<string>> KeyValues)
{
Type entityType = typeof(T);
Expression<Func<T, bool>> expression = null;
ParameterExpression paramExpr = Expression.Parameter(typeof(T), "e");
foreach (var keyItem in KeyValues)
{
foreach (var keyValue in KeyValues[keyItem.Key])
{
Expression<Func<T, bool>> lambda;
string field = keyItem.Key;//关键字
PropertyInfo proInfo = entityType.GetProperty(field);
MemberExpression mExpr = Expression.Property(paramExpr, field);  
ConstantExpression cExpr = Expression.Constant(keyValue, proInfo.PropertyType);  
BinaryExpression bExpr = Expression.Equal(mExpr, cExpr);   
Expression whereExpr = bExpr;
lambda = Expression.Lambda<Func<T, bool>>(whereExpr, paramExpr);
if (expression != null)
{
expression = expression.ExpressionOr(lambda);
}
else
{
expression = lambda;
}
}
}
return expression;
}

/// <summary>
/// 等于
/// </summary>
/// <typeparam name="T">实体类</typeparam>
/// <param name="key">关键字</param>
/// <param name="keyItem">关键字对应的值</param>
/// <returns></returns>
public static Expression<Func<T, bool>> Equel_Expression<T>(string key, object keyItem)
{

Type entityType = typeof(T);
ParameterExpression paramExpr = Expression.Parameter(typeof(T), "e");
Expression<Func<T, bool>> lambda;
lambda = Equel128_GetExpression<T>(key, keyItem, true);
if (lambda != null)
{
return lambda;
}
string field = key;//关键字
PropertyInfo proInfo = entityType.GetProperty(field);
var keyValue = keyItem;
MemberExpression mExpr = Expression.Property(paramExpr, field);  
ConstantExpression cExpr = Expression.Constant(keyValue, proInfo.PropertyType);

BinaryExpression bExpr = Expression.Equal(mExpr, cExpr);

Expression whereExpr = bExpr;
lambda = Expression.Lambda<Func<T, bool>>(whereExpr, paramExpr);
return lambda;
}

/// <summary>
/// 大于
/// </summary>
/// <typeparam name="T">实体类</typeparam>
/// <param name="key">关键字</param>
/// <param name="keyItem">关键字对应的值</param>
/// <returns></returns>
public static Expression<Func<T, bool>> Greaterthan_Expression<T>(string key, object keyItem)
{
Type entityType = typeof(T);
ParameterExpression paramExpr = Expression.Parameter(typeof(T), "e");
Expression<Func<T, bool>> lambda;
string field = key;//关键字
PropertyInfo proInfo = entityType.GetProperty(field);
var keyValue = keyItem;
MemberExpression mExpr = Expression.Property(paramExpr, field);  
ConstantExpression cExpr = Expression.Constant(keyValue, proInfo.PropertyType);  
BinaryExpression bExpr = Expression.GreaterThan(mExpr, cExpr);   
Expression whereExpr = bExpr;
lambda = Expression.Lambda<Func<T, bool>>(whereExpr, paramExpr);

return lambda;
}
/// <summary>
/// 大于等于
/// </summary>
/// <typeparam name="T">实体类</typeparam>
/// <param name="key">关键字</param>
/// <param name="keyItem">关键字对应的值</param>
/// <returns></returns>
public static Expression<Func<T, bool>> GreaterThanOrEqual_Expression<T>(string key, object keyItem)
{
Type entityType = typeof(T);
ParameterExpression paramExpr = Expression.Parameter(typeof(T), "e");
Expression<Func<T, bool>> lambda;
string field = key;//关键字
PropertyInfo proInfo = entityType.GetProperty(field);
var keyValue = keyItem;
MemberExpression mExpr = Expression.Property(paramExpr, field);  
ConstantExpression cExpr = Expression.Constant(keyValue, proInfo.PropertyType);  
BinaryExpression bExpr = Expression.GreaterThanOrEqual(mExpr, cExpr);   
Expression whereExpr = bExpr;
lambda = Expression.Lambda<Func<T, bool>>(whereExpr, paramExpr);

return lambda;
}

/// <summary>
/// 小于
/// </summary>
/// <typeparam name="T">实体类</typeparam>
/// <param name="key">关键字</param>
/// <param name="keyItem">关键字对应的值</param>
/// <returns></returns>
public static Expression<Func<T, bool>> LessThan_Expression<T>(string key, object keyItem)
{
Type entityType = typeof(T);
ParameterExpression paramExpr = Expression.Parameter(typeof(T), "e");
Expression<Func<T, bool>> lambda;
string field = key;//关键字
PropertyInfo proInfo = entityType.GetProperty(field);
var keyValue = keyItem;
MemberExpression mExpr = Expression.Property(paramExpr, field);  
ConstantExpression cExpr = Expression.Constant(keyValue, proInfo.PropertyType);  
BinaryExpression bExpr = Expression.LessThan(mExpr, cExpr);   
Expression whereExpr = bExpr;
lambda = Expression.Lambda<Func<T, bool>>(whereExpr, paramExpr);
return lambda;
}

/// <summary>
/// 小于等于
/// </summary>
/// <typeparam name="T">实体类</typeparam>
/// <param name="key">关键字</param>
/// <param name="keyItem">关键字对应的值</param>
/// <returns></returns>
public static Expression<Func<T, bool>> LessThanOrEqual_Expression<T>(string key, object keyItem)
{
Type entityType = typeof(T);
ParameterExpression paramExpr = Expression.Parameter(typeof(T), "e");
Expression<Func<T, bool>> lambda;
string field = key;//关键字
PropertyInfo proInfo = entityType.GetProperty(field);
var keyValue = keyItem;
MemberExpression mExpr = Expression.Property(paramExpr, field);  
ConstantExpression cExpr = Expression.Constant(keyValue, proInfo.PropertyType);  
BinaryExpression bExpr = Expression.LessThanOrEqual(mExpr, cExpr);   
Expression whereExpr = bExpr;
lambda = Expression.Lambda<Func<T, bool>>(whereExpr, paramExpr);
return lambda;
}

/// <summary>
/// 不等于
/// </summary>
/// <typeparam name="T">实体类</typeparam>
/// <param name="key">关键字</param>
/// <param name="keyItem">关键字对应的值</param>
/// <returns></returns>
public static Expression<Func<T, bool>> NotEqual_Expression<T>(string key, object keyItem)
{
Type entityType = typeof(T);
ParameterExpression paramExpr = Expression.Parameter(typeof(T), "e");
Expression<Func<T, bool>> lambda;
lambda = Equel128_GetExpression<T>(key, keyItem, false);
if (lambda != null)
{
return lambda;
}
string field = key;//关键字
PropertyInfo proInfo = entityType.GetProperty(field);
var keyValue = keyItem;
MemberExpression mExpr = Expression.Property(paramExpr, field);  
ConstantExpression cExpr = Expression.Constant(keyValue, proInfo.PropertyType);  
BinaryExpression bExpr = Expression.NotEqual(mExpr, cExpr);   
Expression whereExpr = bExpr;
lambda = Expression.Lambda<Func<T, bool>>(whereExpr, paramExpr);
return lambda;
}

#region 自定义字段
/// <summary>
///判断 decimal128 格式并进行处理
/// </summary>
/// <typeparam name="T">实体</typeparam>
/// <param name="name">关键字</param>
/// <param name="keyItem">值</param>
/// <param name="isEquel">是否相等 true等于 false 不等于</param>
/// <returns></returns>
private static Expression<Func<T, bool>> Equel128_GetExpression<T>(string name, object keyItem, bool isEquel)
{
Expression<Func<T, bool>> lambda = null;
Type entityType = typeof(T);
var param = Expression.Parameter(typeof(T), "x");
PropertyInfo proInfo = entityType.GetProperty(name);
var type = proInfo.PropertyType.FullName;
try
{
if (type.Contains("System.Decimal") && type != "System.Decimal")//包含但是不等于相当于 为decimal? 格式。否则相当于decimal格式
{
Decimal128 kk = Decimal128.Parse(keyItem.ToString());
BsonDecimal128 aa = new BsonDecimal128(kk);
ParameterExpression parameter = Expression.Parameter(typeof(T), "p");
MemberExpression member = Expression.PropertyOrField(parameter, name);
var addMethod = typeof(decimal?).GetMethod("Equals");
ConstantExpression constant = Expression.Constant(aa, typeof(BsonDecimal128));
if (isEquel)
{
lambda = Expression.Lambda<Func<T, bool>>(Expression.Call(member, addMethod, constant), parameter);
}
else
{
lambda = Expression.Lambda<Func<T, bool>>(Expression.Not(Expression.Call(member, addMethod, constant)), parameter);
}
}
}
catch (Exception ex)
{
lambda = null;
}
return lambda;
}
#endregion

}
public class MyExpressionVisitor : ExpressionVisitor
{
public ParameterExpression _Parameter { get; set; }

public MyExpressionVisitor(ParameterExpression Parameter)
{
_Parameter = Parameter;
}
protected override Expression VisitParameter(ParameterExpression p)
{
return _Parameter;
}

public override Expression Visit(Expression node)
{
return base.Visit(node);//Visit会根据VisitParameter()方法返回的Expression修改这里的node变量
}
}

ExpressionHelp2的更多相关文章

随机推荐

  1. 利用Apache部署静态网站(二)

    本文接着<利用Apache部署静态网站(一)>继续部署,为系统中的每位用户创建一个独立的网站. httpd服务程序提供的个人用户主页功能可以为每位用户创建一个独立的网站.该功能可以让系统内 ...

  2. Jenkins 系统管理与配置

    1. Jenkins 安装插件的两种方式 2. 添加凭据(Credentials) 3. 系统管理--全局工具配置 4. 系统管理--系统设置 5. 常用插件说明 Extended E-mail No ...

  3. 服务器安装node全教程

    我的服务器centos,安装node时出了点小麻烦,在这里记述我的方法. 1.进入node下载网站https://nodejs.org/en/download/,这里右键复制下载链接 2.进入cent ...

  4. dedecms后台一些时间等验证方法(plus/diy.php)

    <?php if(trim(@$_POST['name'])==''){ $err=2; } if(trim(@$_POST['tel'])==''){ $err=1; }else{ @$_PO ...

  5. Qt链接MySQL发布后Drive not loadedDrive not loaded怎么办

    缺少动态链接库!把MySQL文件夹下面带dll.lib的全复制进去就行了

  6. 初始化mysql报错bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

    原因:缺少libaio.so.1 解决办法:安装即可 yum install -y libaio

  7. 浅析DDD——领域驱动设计的理解

    浅析DDD--领域驱动设计的理解 我觉得领域驱动设计概念的提出,是为了更清晰的区分边界.这里的边界包括业务边界和功能的边界,每个边界都包含具体的领域对象,当业务和功能的领域对象一一对应上之后,业务的变 ...

  8. 初探MFC

    MFC MFC(Microsoft Foundation Classes) 是微软基础类库,也就是用c++类将win32API封装起来. 应用程序对象 MFC程序都是以应用程序对象为核心,且程序中只有 ...

  9. redis 未授权访问(写公钥、写计划任务)

    写公钥 ssh-keygen -t rsa # 生成key (echo -e "\n\n"; cat id_rsa.pub; echo -e "\n\n") & ...

  10. eth-trunk

    ------------恢复内容开始------------ 1.eth-trunk 是什么 *链路 聚合技术 2.做什么用的 *作为一种链路捆绑技术,可以把多个独立物理接口绑定在一起,作为一个大带宽 ...