[2014-12-30]如何动态构造Lambda表达式(动态构造Lambda查询条件表达式)
声明
本文对Lambda表达式的扩展,示例代码来源于网络。
场景描述
web开发查询功能的时候,如果查询条件比较多,就会遇到动态组合查询条件的情况。在手写sql的情况下,我们一般会根据传入的参数,针对where部分进行sql语句的动态组装,而现在在使用EF的时候遇到这个问题,查询条件不再是以sql字符串的形式传递了,而是一个Lambda表达式,那么如何进行Lambda表达式的动态构造呢?
虽然Lambda表达式可以声明为变量,但是要进行表达式累加,目前并没有默认的、好用且方便的方法,参考了很多资料,寻到一剂良方。
先看看效果
先看看扩展后的使用示例:
public ActionResult List(int? page, string where)
{
Expression<Func<ComArticle, bool>> Conditions = PredicateExtensions.True<ComArticle>();
Conditions = Conditions.And(x => x.Shenhe == true && x.Company.City.CityId == CurrentCity.CityId);
int t_id = 0;
if (int.TryParse(where.GetByName("t"), out t_id) && t_id > 0 && t_id < 7)
{
var tmp = (EN_ArticleType)Enum.Parse(typeof(EN_ArticleType), t_id + "");
Conditions = Conditions.And(x => x.Type == tmp);
}
var items = articleRepo.GetMany(Conditions).OrderByDescending(a => a.AddTime).ToPagedList(page ?? 1, 5);
return View(items);
}
如果有传入的t_id则Conditions会通过And条件累加Type筛选。
这里的关键在于对Expression的扩展,和PredicateExtensions.True()这个东西。
那么,我们看看PredicateExtensions的代码。
扩展
简单的扩展,受益于ExpressionVisitor。
public class ParameterRebinder : ExpressionVisitor
{
private readonly Dictionary<ParameterExpression, ParameterExpression> map;
public ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
{
this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
}
public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
{
return new ParameterRebinder(map).Visit(exp);
}
protected override Expression VisitParameter(ParameterExpression p)
{
ParameterExpression replacement;
if (map.TryGetValue(p, out replacement))
{
p = replacement;
}
return base.VisitParameter(p);
}
}
public static class PredicateExtensions
{
public static Expression<Func<T, bool>> True<T>() { return f => true; }
public static Expression<Func<T, bool>> False<T>() { return f => false; }
public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
{
// build parameter map (from parameters of second to parameters of first)
var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f);
// replace parameters in the second lambda expression with parameters from the first
var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
// apply composition of lambda expression bodies to parameters from the first expression
return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
}
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.And);
}
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.Or);
}
}
ExpressionVisitor可以深入了解下。over
[2014-12-30]如何动态构造Lambda表达式(动态构造Lambda查询条件表达式)的更多相关文章
- Lambda 中如果构建一个查询条件,扔该Where返回我们需要的数据。
有一个需求,比如所 省市县 这三个查询条件 都可能有可能没有,但是我们的查询条件怎么构建呢 首先需要看一下 Lambda中Where这个方法需要什么参数 public static IEnumerab ...
- Oracle的查询-条件表达式
给emp表中员工起中文名 select e.ename from emp e; select e.ename, case e.ename when 'SMITH' then '曹贼' when 'AL ...
- Struts2的Action名称搜索顺序:2014.12.30
struts.xml配置: <struts> <package name="hw" namespace="/test" extends=&qu ...
- 查询表格——建立动态表格,使用ajax输入查询条件将后台数据查询出来以表格的形式展示出来
建立动态表格,使用ajax将前台查询条件传给后台,并将查询结果以表格的形式展示出来. 页面的展示效果如下图所示: 第一步:查询条件的部分: 代码如下: <div class="text ...
- shell(shell变量、条件表达式、流程控制)
本章内容: 变量 运算 if语句 for语句 while语句 break.continue 实例 shell变量 1.shell变量简介 变量是任何一种编程语言都必不可少的组成部分,变量用来存放各种数 ...
- [Inside HotSpot] C1编译器优化:条件表达式消除
1. 条件传送指令 日常编程中有很多根据某个条件对变量赋不同值这样的模式,比如: int cmov(int num) { int result = 10; if(num<10){ result ...
- python条件表达式:多项分支,双向分支
# ### 多项分支 ''' if 条件表达式1: code1 code2 elif 条件表达式2: code3 code4 elif 条件表达式3: code5 code6 else: code7 ...
- if语句的嵌套以及条件运算符和条件表达式(初学者)
1.当if语句中的执行语句又是if语句时,则构成了if语句的嵌套情形. 其一般形式可表示为: if() { if()……; } 或: if() if()语句1: else 语句2: else if() ...
- C#在泛型类中,通过表达式树构造lambda表达式
场景 最近对爬虫的数据库架构做调整,需要将数据迁移到MongoDB上去,需要重新实现一个针对MongoDB的Dao泛型类,好吧,动手开工,当实现删除操作的时候问题来了. 我们的删除操作定义如下:voi ...
随机推荐
- mysql中csv文件的导入导出
导出例子: select *from testinto outfile 'D:test.csv'fields terminated by ',' optionally enclosed by '&qu ...
- (转)Linux开机启动(bootstrap)
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 计算机开机是一个神秘的过程.我们只是按了开机键,就看到屏幕上的进度条或者一行行的输 ...
- [js] 实现接口
摘要 js 语言并没有interface implement 关键字,如果要做到和后端语言类似的接口,一般会需要模拟实现. 在oo 编程中, 接口可以让架构师规定好模块必须要实现的方法, 尽量解耦模块 ...
- 【PHP】基础学习
摘要: 基础知识回顾 1)数组 2)字符串 3)时间 一.数组 1.1 数组分类索引数组:常规数组关联数组:键值对数组 二.字符串 2.1 定义字符串heredoc语法结构定义的字符串:$hello ...
- javascript中this的用法
this是Javascript语言的一个关键字. 它代表函数运行时,自动生成的一个内部对象,只能在函数内部使用.比如, function test(){ this.x = 1; } 随着函数使用场合的 ...
- js一些重点知识总结(二)
第一部分:数据类型转换 1) 数据类型的种类: 数值型number.布尔型(true(1) /false (0)).字符串型(String).空类型(null)(object) 2) 数据类型自动转换 ...
- 腾讯 AlloyCrop 1.0 发布
写在前面 AlloyCrop 这个项目是8个月前发布的,作为AlloyFinger 的典型案例,发布之后被BAT等其他公司广泛使用.但是发布之后,有两个问题一直没有抽出时间去解决: 裁剪图像的分辨率太 ...
- 已有 JFFs2文件系统的修改
项目应用中,对于前人留下的JFFS2的文件,有时候我们需要修改,但是苦于没有源文件,实际操作很多时候无所适从.每次支持生产之后再进行人为的升级.这样费时费力,也给生产人员增加了负担. 为了解决这个问题 ...
- MySQL连接问题【如何解决MySQL连接超时关闭】
--MySQL连接问题[如何解决MySQL连接超时关闭] ------------------------------------------------转载 最近做网站有一个站要用到WEB网页采集器 ...
- 【EntityFramework 6.1.3】个人理解与问题记录(3)
前言 说点题外话:前几天接连微软老爹发布了 .net core 2.0 / asp.net core 2.0 / ef core 2.0 / .net standard 2.0(此处撒花,不管是否后面 ...