规约模式的ef拼接
public class LamadaExtention<Dto> where Dto : new()
{
private List<Expression> m_lstExpression = null;
private ParameterExpression m_Parameter = null; public LamadaExtention()
{
m_lstExpression = new List<Expression>();
m_Parameter = Expression.Parameter(typeof(Dto), "x");
} //构造表达式,存放到m_lstExpression集合里面
public void GetExpression(string strPropertyName, object strValue, ExpressionType expressType)
{
Expression expRes = null;
MemberExpression member = Expression.PropertyOrField(m_Parameter, strPropertyName);
if (expressType == ExpressionType.Contains)
{
expRes = Expression.Call(member, typeof(string).GetMethod("Contains"), Expression.Constant(strValue));
}
else if (expressType == ExpressionType.Equal)
{
expRes = Expression.Equal(member, Expression.Constant(strValue, member.Type));
}
else if (expressType == ExpressionType.LessThan)
{
expRes = Expression.LessThan(member, Expression.Constant(strValue, member.Type));
}
else if (expressType == ExpressionType.LessThanOrEqual)
{
expRes = Expression.LessThanOrEqual(member, Expression.Constant(strValue, member.Type));
}
else if (expressType == ExpressionType.GreaterThan)
{
expRes = Expression.GreaterThan(member, Expression.Constant(strValue, member.Type));
}
else if (expressType == ExpressionType.GreaterThanOrEqual)
{
expRes = Expression.GreaterThanOrEqual(member, Expression.Constant(strValue, member.Type));
}
//return expRes;
m_lstExpression.Add(expRes);
} //针对Or条件的表达式
public void GetExpression(string strPropertyName, List<object> lstValue)
{
Expression expRes = null;
MemberExpression member = Expression.PropertyOrField(m_Parameter, strPropertyName);
foreach (var oValue in lstValue)
{
if (expRes == null)
{
expRes = Expression.Equal(member, Expression.Constant(oValue, member.Type));
}
else
{
expRes = Expression.Or(expRes, Expression.Equal(member, Expression.Constant(oValue, member.Type)));
}
} m_lstExpression.Add(expRes);
} //多个字段or同一个值
public void GetExpression(List<string> listStrPropertyName, object strValue, ExpressionType expressType)
{
Expression expRes = null; foreach (var itemValue in listStrPropertyName)
{
MemberExpression member = Expression.PropertyOrField(m_Parameter, itemValue);
if (expressType == ExpressionType.Contains)
{
if (expRes == null)
{
expRes = Expression.Call(member, typeof(string).GetMethod("Contains"), Expression.Constant(strValue));
//expRes = Expression.Equal(member, Expression.Constant(strValue, member.Type));
}
else
{
expRes = Expression.Or(expRes, Expression.Call(member, typeof(string).GetMethod("Contains"), Expression.Constant(strValue)));
}
}
else
{
if (expRes == null)
{
expRes = Expression.Equal(member, Expression.Constant(strValue, member.Type));
}
else
{
expRes = Expression.Or(expRes, Expression.Equal(member, Expression.Constant(strValue, member.Type)));
}
}
}
m_lstExpression.Add(expRes);
} //得到Lamada表达式的Expression对象
public Expression<Func<Dto, bool>> GetLambda()
{
Expression whereExpr = null;
foreach (var expr in this.m_lstExpression)
{
if (whereExpr == null) whereExpr = expr;
else whereExpr = Expression.And(whereExpr, expr);
}
if (whereExpr == null)
return null;
return Expression.Lambda<Func<Dto, Boolean>>(whereExpr, m_Parameter);
}
} //用于区分操作的枚举
public enum ExpressionType
{
Contains,//like
Equal,//等于
LessThan,//小于
LessThanOrEqual,//小于等于
GreaterThan,//大于
GreaterThanOrEqual//大于等于
}
使用:
var oLamadaExtention = new LamadaExtention<CommonCase>(); //2.依次构造Lamada表达式
if (!string.IsNullOrEmpty(key))
{
List<string> lstValue = new List<string>();
lstValue.Add("Title");
lstValue.Add("KeyWord");
oLamadaExtention.GetExpression(lstValue, key,ExpressionType.Contains);
} if (!string.IsNullOrEmpty(createSTime))
{
var time = Convert.ToDateTime(createSTime);
oLamadaExtention.GetExpression("CreateTime", time, ExpressionType.GreaterThanOrEqual);
}
if (!string.IsNullOrEmpty(createETime))
{
var time = Convert.ToDateTime(createETime);
oLamadaExtention.GetExpression("CreateTime", time, ExpressionType.LessThanOrEqual);
}
规约模式的ef拼接的更多相关文章
- [.NET领域驱动设计实战系列]专题三:前期准备之规约模式(Specification Pattern)
一.前言 在专题二中已经应用DDD和SOA的思想简单构建了一个网上书店的网站,接下来的专题中将会对该网站补充更多的DDD的内容.本专题作为一个准备专题,因为在后面一个专题中将会网上书店中的仓储实现引入 ...
- 规约模式(Specification Pattern)
前期准备之规约模式(Specification Pattern) 一.前言 在专题二中已经应用DDD和SOA的思想简单构建了一个网上书店的网站,接下来的专题中将会对该网站补充更多的DDD的内容.本专题 ...
- 生产环境下实践DDD中的规约模式
最近的开发工作涉及到两个模块“任务”和“日周报”.关系是日周报消费任务,因为用户在写日周报的时候,需要按一定的规则筛选当前用户的任务,作为日周报的一部分提交.整个项目采用类似于Orchard那种平台加 ...
- [.NET领域驱动设计实战系列]专题五:网上书店规约模式、工作单元模式的引入以及购物车的实现
一.前言 在前面2篇博文中,我分别介绍了规约模式和工作单元模式,有了前面2篇博文的铺垫之后,下面就具体看看如何把这两种模式引入到之前的网上书店案例里. 二.规约模式的引入 在第三专题我们已经详细介绍了 ...
- DDD~领域服务的规约模式
回到目录 规 约(Specification)模式:第一次看到这东西是在microsoft NLayer项目中,它是微软对DDD的解说,就像petshop告诉了我们MVC如何使用一样,这个规约模式最重 ...
- 规约模式(Specification Pattern)
一.引言 最近在看一个项目的源码时(DDD),对里面的一些设计思想和设计思路有了一些疑问.当看到(Repository层)中使用了 spec.SatisfiedBy() 时,感觉有点懵.于是在项目中搜 ...
- step_by_step_ABP规约模式
一段时间没有在github 上浏览ABP项目,几天前看到ABP新增规约模式,开始了解并学习文档 记录一下 Introduction 介绍 Specification pattern is a pa ...
- 规约模式Specification Pattern
什么是规约模式 规约模式允许我们将一小块领域知识封装到一个单元中,即规约,然后可以在code base中对其进行复用. 它可以用来解决在查询中泛滥着GetBySomething方法的问题,以及对查询条 ...
- 设计模式:规约模式(Specification-Pattern)
"其实地上本没有路,走的人多了,也便成了路"--鲁迅<故乡> 这句话很好的描述了设计模式的由来.前辈们通过实践和总结,将优秀的编程思想沉淀成设计模式,为开发者提供了解决 ...
随机推荐
- 《Maven实战》关联实际工作的核心知识
通读了<Maven实战>这本书,由于在实际的工作中,对其有一定的操作上的经验.因此,再回头去通读这本书,就能够更加精准的把握里面的核心知识了. 以下我主要从两点去介绍之—— 1> m ...
- html空白文字宽度
原文链接 名称 编号 描述 不断行的空白(1个字符宽度) 半个空白(1个字符宽度) 一个空白(2个字符宽度) 窄空白(小于1个字符宽度) 小写加分号!
- Swift数据类型_整型和浮点型
//swift中的整型和浮点型 /** * //类型推断整数是Int 浮点数是Double ,日常使用需要注意不能越界,存储时间毫秒数 英雄经验数等等之类内容容易越界 整型 大多数情况下,你不需要在代 ...
- MyBatis 指定的转换无效
表字段Pay类型设置的是float,生成类的属性如下: public double Pay{get;set;} 读取列表时出现如下错误: 错误信息: 查看堆栈跟踪信息, get_Decimal()提示 ...
- sun.misc.unsafe
Java中大部分错误都是基于内存管理方面的.如果想破坏,可以使用Unsafe这个类. 实例化Unsafe: 下面两种方式是不行的 private Unsafe() {} //私有构造方法 @Calle ...
- 子级用css float浮动 而父级不能自适应高度解决方法
解决子级对象使用css float浮动 而父级div不能自适应高度,不能被父级内容撑开解决方法,父级div没有高度解决方法. 当在对象内的盒子使用了float后,导致对象本身不能被撑开自适应高度,这个 ...
- 原生js、jQuery实现选项卡功能
在大家在网上平常浏览网页的时候,想必各位都会看到选项卡功能,在这里给大家详解一下用原生js.jQuery如何来写一些基本的选项卡 话不多说,先给各位看一下功能图: 好了,下边 ...
- div,css&table布局有哪些区别
DIV+CSS布局与TABLE布局相比,有哪些优点? 1.代码少,页面文件小,下载快 Div+css的布局现在属于国际W3C标准,table不是. 都知道用div的布局代码肯定少,所有的样式都在CSS ...
- Linux基础之-正则表达式(grep,sed,awk)
一. 正则表达式 正则表达式,又称规则表达式.(英语:Regular Expression,在代码中常简写为regex.regexp或RE),计算机科学的一个概念.正则表达式是对字符串操作的一种逻辑公 ...
- .NET开源工作流RoadFlow-Bug修改-1.8.2子流程接收者始终为发送者
1.8.2及以前版本中子流程待办任务的处理者始终为上一步骤发送者BUG的处理: 修改类:RoadFlow.Platform.WorkFlowTask中如下图红框中的内容即可: