/// <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, 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.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);
}
/// <summary>
/// 创建lambda表达式:p=>p.propertyName.Contains(propertyValue)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="column"></param>
/// <param name="value"></param>
/// <returns></returns>
private 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>
private 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);
}
/// <summary>
/// 组合And
/// </summary>
/// <returns></returns>
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.AndAlso);
}
/// <summary>
/// 组合Or
/// </summary>
/// <returns></returns>
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.OrElse);
}
/// <summary>
/// Combines the first expression with the second using the specified merge function.
/// </summary>
static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
{
var map = first.Parameters
.Select((f, i) => new { f, s = second.Parameters[i] })
.ToDictionary(p => p.s, p => p.f);
var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
}
/// <summary>
/// ParameterRebinder
/// </summary>
private class ParameterRebinder : ExpressionVisitor
{
/// <summary>
/// The ParameterExpression map
/// </summary>
readonly Dictionary<ParameterExpression, ParameterExpression> map;
/// <summary>
/// Initializes a new instance of the <see cref="ParameterRebinder"/> class.
/// </summary>
/// <param name="map">The map.</param>
ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
{
this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
}
/// <summary>
/// Replaces the parameters.
/// </summary>
/// <param name="map">The map.</param>
/// <param name="exp">The exp.</param>
/// <returns>Expression</returns>
public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
{
return new ParameterRebinder(map).Visit(exp);
}
/// <summary>
/// Visits the parameter.
/// </summary>
/// <param name="p">The p.</param>
/// <returns>Expression</returns>
protected override Expression VisitParameter(ParameterExpression p)
{
ParameterExpression replacement;
if (map.TryGetValue(p, out replacement))
{
p = replacement;
}
return base.VisitParameter(p);
}
}

C#动态创建lambda表达式的更多相关文章

  1. 动态创建Lambda表达式实现高级查询

    需求简介 最近这几天做的东西总算是回归咱的老本行了,给投资管理项目做一个台账的东西,就是类似我们的报表.其 中有一个功能是一个高级查询的需求,在查询条件方面大概有7.8个查询条件.需求就是如果一个条件 ...

  2. 使用Expression动态创建lambda表达式

    using System;using System.Linq.Expressions;using System.Reflection; namespace Helper{ public class L ...

  3. 动态创建 Lambda 表达式

    首先我们看一个简单 Lambda 表达式的构成. i => i > 5 在这个表达式中,"i" 被称为 Parameter,"i > 5" 是 ...

  4. 动态组合lambda 表达式

    //记录实体集合—动态组合lambda 表达式 Expression<Func<AdEntity, bool>> thirdWhere = p => p.Observer ...

  5. easyui datagrid remoteSort的实现 Controllers编写动态的Lambda表达式 IQueryable OrderBy扩展

    EF 结合easy-ui datagrid 实现页面端排序 EF动态编写排序Lambda表达式 1.前端页面 var mainListHeight = $(window).height() - 20; ...

  6. 【转】EntityFramework动态组合Lambda表达式作为数据筛选条件,代替拼接SQL语句

    传统的操作数据库方式,筛选数据需要用StringBuilder拼接一大堆的WHERE子句. 在Entity Framework中,代码稍有不慎就会造成巨大性能消耗,如: using(var db=ne ...

  7. 动态拼接lambda表达式树

    前言 最近在优化同事写的代码(我们的框架用的是dapperLambda),其中有一个这样很普通的场景——界面上提供了一些查询条件框供用户来进行过滤数据.由于dapperLambda按条件查询时是传入表 ...

  8. [2014-12-30]如何动态构造Lambda表达式(动态构造Lambda查询条件表达式)

    声明 本文对Lambda表达式的扩展,示例代码来源于网络. 场景描述 web开发查询功能的时候,如果查询条件比较多,就会遇到动态组合查询条件的情况.在手写sql的情况下,我们一般会根据传入的参数,针对 ...

  9. 动态构建Lambda表达式实现EF动态查询

    在使用Entity Framework做数据查询的时候,查询条件往往不是固定的,需要动态查询.可以通过动态构建Lamda表达式来实现动态查询. Lamda表达式 使用Lamda表达式可以很方便的按条件 ...

随机推荐

  1. python 以行为单位进行字符串的切割

    可以使用str 的 splitlines() 方法 实现以行为单位 进行字符串的切割, keepends=False 不保留\n符号, kendends=True 保留\n符号 my_str = &q ...

  2. Access 2010 应用基础 单元三:SQL查询

    导语:Access查询中拉差距的部分 简单查询 [是基于单个表的查询] 无条件从数据表中选择部分字段 Select 字段列表 from 数据表 无条件从数据表中选择全部字段 Select 字段列表 f ...

  3. Windows下的Nginx安装与配置(PHP)

    因为一直用Apache作为服务器,对Apache的使用和配置已经相对熟悉,今天换了一下nginx的服务器,整个配置流程相对比较简单,php的配置没有任何变化. 主要的参考文档为 http://blog ...

  4. css基础小总结

    header{font-size:1em;padding-top:1.5em;padding-bottom:1.5em} .markdown-body{overflow:hidden} .markdo ...

  5. centos7 防火墙与端口设置、linux端口范围

    防火墙 启动防火墙: systemctl start firewalld 查看防火墙状态: systemctl status firewalld 关闭防火墙: systemctl stop firew ...

  6. Junit的常见注解

    @Beforeclass:表示使用此注解的方法在测试类被调用之前执行,在一个测试类中只能声明此注解一次,此注解的方法只被执行一次 @AfterClass :表示使用此注解的方法在测试类被调用结束退出之 ...

  7. js集合set类的实现

    js集合set类的实现 /*js集合set类的实现*/ function Set() { this.dataStore = []; this.add = add;//新增元素 this.remove ...

  8. jade直接写类似JavaScript语法的东西,不需要写script

    我们知道,html做计算都是在JavaScript中完成的,那么不用JavaScript行不行呢,可以直接在jade中一样的编写 如: -var a = 3 -var b = 4 div a+b = ...

  9. vue-Treeselect 使用备注

    <head> <!-- include Vue 2.x --> <script src="https://cdn.jsdelivr.net/npm/vue@@^ ...

  10. 关于冒泡排序的Java代码实现

    一.排序算法的历史: 排序算法的发展历史几乎和计算机的发展历史一样悠久,而且直到今天,世界范围内依然有计算机科学家正在研究着排序的算法,由此可见排序算法的强大魅力.   我们现在介绍的排序算法都是前任 ...