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拼接的更多相关文章

  1. [.NET领域驱动设计实战系列]专题三:前期准备之规约模式(Specification Pattern)

    一.前言 在专题二中已经应用DDD和SOA的思想简单构建了一个网上书店的网站,接下来的专题中将会对该网站补充更多的DDD的内容.本专题作为一个准备专题,因为在后面一个专题中将会网上书店中的仓储实现引入 ...

  2. 规约模式(Specification Pattern)

    前期准备之规约模式(Specification Pattern) 一.前言 在专题二中已经应用DDD和SOA的思想简单构建了一个网上书店的网站,接下来的专题中将会对该网站补充更多的DDD的内容.本专题 ...

  3. 生产环境下实践DDD中的规约模式

    最近的开发工作涉及到两个模块“任务”和“日周报”.关系是日周报消费任务,因为用户在写日周报的时候,需要按一定的规则筛选当前用户的任务,作为日周报的一部分提交.整个项目采用类似于Orchard那种平台加 ...

  4. [.NET领域驱动设计实战系列]专题五:网上书店规约模式、工作单元模式的引入以及购物车的实现

    一.前言 在前面2篇博文中,我分别介绍了规约模式和工作单元模式,有了前面2篇博文的铺垫之后,下面就具体看看如何把这两种模式引入到之前的网上书店案例里. 二.规约模式的引入 在第三专题我们已经详细介绍了 ...

  5. DDD~领域服务的规约模式

    回到目录 规 约(Specification)模式:第一次看到这东西是在microsoft NLayer项目中,它是微软对DDD的解说,就像petshop告诉了我们MVC如何使用一样,这个规约模式最重 ...

  6. 规约模式(Specification Pattern)

    一.引言 最近在看一个项目的源码时(DDD),对里面的一些设计思想和设计思路有了一些疑问.当看到(Repository层)中使用了 spec.SatisfiedBy() 时,感觉有点懵.于是在项目中搜 ...

  7. step_by_step_ABP规约模式

    一段时间没有在github 上浏览ABP项目,几天前看到ABP新增规约模式,开始了解并学习文档   记录一下 Introduction 介绍 Specification pattern is a pa ...

  8. 规约模式Specification Pattern

    什么是规约模式 规约模式允许我们将一小块领域知识封装到一个单元中,即规约,然后可以在code base中对其进行复用. 它可以用来解决在查询中泛滥着GetBySomething方法的问题,以及对查询条 ...

  9. 设计模式:规约模式(Specification-Pattern)

    "其实地上本没有路,走的人多了,也便成了路"--鲁迅<故乡> 这句话很好的描述了设计模式的由来.前辈们通过实践和总结,将优秀的编程思想沉淀成设计模式,为开发者提供了解决 ...

随机推荐

  1. KMeans实现

    KMeans实现 符号 \(K\): 聚类的个数 \(x^{(i)}\): 第i个样本 \(\mu_{1},\mu_{2},...\mu_{K}\): K个中心节点 \(c^{(i)}\): 第i个样 ...

  2. step2: 爬取廖雪峰博客

    #https://zhuanlan.zhihu.com/p/26342933 #https://zhuanlan.zhihu.com/p/26833760 scrapy startproject li ...

  3. js验证港澳居民通行证号码是否合规

    需求:最近要做实名验证的功能,但是验证我们要验证严谨一点,参考了网上关于验证港澳居民通行证号码的代码,总结一下. 代码: function checkHKMacao(code){ var tip = ...

  4. Firebird3 embedded connection

    Firebird3 的嵌入式连接和以前的版本不同,官方也不再单独发布嵌入式版本了,因为嵌入式版本已经包含在zip包里了. 具体只需要文件: fbclient.dll.ib_util.dll.icudt ...

  5. 浅谈MES系统SMT的JIT功能(二):JIT流程

    上周说到JIT的原理,今天就说说JIT功能的基本流程:从维护基本信息——>生产人员排程——>仓库人员发料——>生产上料——>JIT物料配送看板拉到仓库人员发第二次料,循环上料发 ...

  6. spring security入门

    1. Restful API和传统API的区别 用URL描述资源 用http描述方法行为,用http状态码描述结果 使用json交互数据 RESTful是一种风格,不是强制的标准 2. 使用sprin ...

  7. vs2017启动调试,点击浏览器或输入后回车浏览器闪退,调试中断

    vs2017在启动调试后,浏览器运行,点击地址栏刚输入几个字符,mmmmm居然闪退了! 什么情况呢?测试一下,换其他浏览器进行调试,偶尔不会有问题, 可是第二天......还是一下 于是浏览器——ww ...

  8. Rabbit简单队列模式

    1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/ ...

  9. LeetCode Find Peak Element [TBD]

    说要写成对数时间复杂度,算了想不出来,写个O(n)的水了 class Solution { public: int findPeakElement(const vector<int> &a ...

  10. COGS2216 你猜是不是KMP

    第一道自己写的FFT...... 不知为啥这题在网上找不到题解......真是麻烦,害得我推了半天...... 还是写个简要题解吧...... 首先把S和T拆成序列,a~z分别对应成1~26,?是0, ...