EntityFramework使用动态Lambda表达式筛选数据
public static class PredicateBuilder
{ 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);
}
}
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);
}
}
下面是调用:
ICollection<Procedure> list;
var predicate = PredicateBuilder.True<Procedure>();
predicate = predicate.And(c => c.DeleteState == );
if (date != null)
{
DateTime nextDay = (DateTime)date;
DateTime dt = new DateTime(nextDay.Year, nextDay.Month, nextDay.Day, , , );
predicate = predicate.And(c => c.ProcedureDate <= dt);
}
if (!string.IsNullOrEmpty(patientUR))
{
predicate = predicate.And(c => c.Patient.PatientReference.Contains(patientUR));
}
if (!string.IsNullOrEmpty(lastName))
{
predicate = predicate.And(c => c.Patient.LastName.Contains(lastName));
}
list = _RepoProcedure.All(predicate).ToList();
EntityFramework使用动态Lambda表达式筛选数据的更多相关文章
- SqlDataReader生成动态Lambda表达式
上一扁使用动态lambda表达式来将DataTable转换成实体,比直接用反射快了不少.主要是首行转换的时候动态生成了委托. 后面的转换都是直接调用委托,省去了多次用反射带来的性能损失. 今天在对Sq ...
- 生成动态Lambda表达式1
SqlDataReader生成动态Lambda表达式 上一扁使用动态lambda表达式来将DataTable转换成实体,比直接用反射快了不少.主要是首行转换的时候动态生成了委托. 后面的转换都是直接调 ...
- 【转】EntityFramework动态组合Lambda表达式作为数据筛选条件,代替拼接SQL语句
传统的操作数据库方式,筛选数据需要用StringBuilder拼接一大堆的WHERE子句. 在Entity Framework中,代码稍有不慎就会造成巨大性能消耗,如: using(var db=ne ...
- C# 构建动态Lambda表达式
做CURD开发的过程中,通常都会需要GetList,然而查询条件是一个可能变化的需求,如何从容对应需求变化呢? 首先,我们来设计一个套路,尝试以最小的工作量完成一次查询条件的需求变更 1.UI收集查询 ...
- Lambda表达式动态拼接(备忘)
EntityFramework动态组合Lambda表达式作为数据筛选条件,代替拼接SQL语句 分类: C# Lambda/Linq Entity Framework 2013-05-24 06:58 ...
- 动态LINQ(Lambda表达式)构建
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...
- 十二、C# 委托与Lambda表达式(匿名方法的另一种写法)
委托与Lambda表达式 1.委托概述 2.匿名方法 3.语句Lambda 4.表达式Lambda 5.表达式树 一.委托概述 相当于C++当中的方法指针,在C#中使用delegate 委托来 ...
- 《C#本质论》读书笔记(12)委托和Lambda表达式
12.1.委托概述 12.1.2 委托的数据类型 为了减少重复代码数量,可以将比较方法作为参数传递给 BubbleSort()方法.此外,为了将方法作为参数传递,必须有一个能够标识方法的数据类型--也 ...
- 动态Lambda表达式打印HelloWorld
最近在用C#与数据库打交道.开发过程中采用了ORM模型(以前是纯sql玩法,复杂的逻辑用存储过程做). 为了能通过配置文件动态地查询字段,也就是说需要能这样写: db.AsQuery<T> ...
随机推荐
- zTree自定义节点name显示样式
//setting中配置属性 view: { showLine:false, dblClickExpand: true, showIcon: false, addDiyDom: addDiyDom / ...
- Sun Solairs系统添加裸设备
Sun Solaris系统添加裸设备对表空间进行扩容的大体操作步骤. 1.查看当前卷组信息 --查看当前卷组信息#vxdg list --查看当前卷组中物理磁盘的使用情况#vxdg free |gre ...
- Windows自带强大的入侵检测工具——Netstat 命令 查询是否中木马
Netstat命令可以帮助我们了解网络的整体使用情况.根据Netstat后面参数的不同,它可以显示不同的网络连接信息.Netstat的参数如图,下面对其中一些参数进行说明.如何检测本机是否有被中木马, ...
- ASP.NET Web API 跨域访问(CORS)要注意的地方
一.客户端用JSONP请求数据 如果你想用JSONP来获得跨域的数据,WebAPI本身是不支持javascript的callback的,它返回的JSON是这样的: {"YourSignatu ...
- 泛微e-cology和Oracle无法启动的解决方案
最近公司的泛微OA无法访问,Oracle数据库也无法正常启动,尝试了好多方法,终于解决了,先说说基本情况,希望能给碰到同样问题的朋友带来一点帮助. 服务器操作系统:Window s Server 20 ...
- JS和css实现检测移动设备方向的变化并判断横竖屏幕
这篇文章主要介绍了JS和css实现检测移动设备方向的变化并判断横竖屏幕,本文分别给出实现代码,需要的朋友可以参考下 方法一:用触发手机的横屏和竖屏之间的切换的事件 [自测可用, chrome , 手 ...
- Promise & Deferred Objects in JavaScript Pt.2: in Practice
原文:http://blog.mediumequalsmessage.com/promise-deferred-objects-in-javascript-pt2-practical-use Intr ...
- [DAViCHi/SeeYa/T-ARA][원더우먼][Wonder Woman]
歌词来源:http://music.163.com/#/song?id=5371229 作曲 : 赵英秀 [作曲 : 赵英秀] [作曲 : 赵英秀] 作词 : K-Smith [作词 : KSmith ...
- 【转载】QPS,用户平均等待时间,服务器平均请求处理时间
转自:http://www.cnblogs.com/coldplayerest/archive/2012/06/28/2567656.html 1. 计算网络的QPS时,必须要指定并发度,否则没有意义 ...
- 用pymysql代替MySQLdb
在我刚开始学python的时候,用的是python2.7,那时候连接mysql用的库是MySQLdb(很诡异的大小写,初学者经常因为记不住大小写导致“No module named xxx”).燃鹅, ...