using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions; namespace HYS.Fee.AppSrv
{
/// <summary>
/// 数据库上下文
/// </summary>
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);
}
}
}

用法:

Expression<Func<表名, bool>> expr = n => true;
expr = expr.And(n => n.PNAME.Contains(""));
expr = expr.Or(n => n.PID > );
context.表名.Where(expr).ToList()

EF Expression 扩展的更多相关文章

  1. .NetCore 使用 Linq 动态拼接Expression表达式条件来实现 对EF、EF Core 扩展查询排序操作

    相信在使用EF的时候对查询条件或者排序上的处理令人心烦,下面我们就来动态拼接表达式解决这一问题 当我们在查询中使用Where的时候可以看到如下参数 下面我们就来扩展 Expression<Fun ...

  2. EF操作扩展之async

    EF的操作类网上很多类,我只是把我在平时项目中的类进行一些改进和扩展,扩展了部分同步和异步的EF操作 接口 /// <summary> /// 接口数据操作基础类 /// </sum ...

  3. 采用EntityFramework.Extended 对EF进行扩展(Entity Framework 延伸系列2)

    前言 Entity Framework 延伸系列目录 今天我们来讲讲EntityFramework.Extended 首先科普一下这个EntityFramework.Extended是什么,如下: 这 ...

  4. EF架构~扩展一个分页处理大数据的方法

    回到目录 最近总遇到大数据的问题,一次性处理几千万数据不实际,所以,我们需要对大数据进行分块处理,或者叫分页处理,我在EF架构里曾经写过类似的,那是在进行BulkInsert时,对大数据批量插入时候用 ...

  5. c# Expression 扩展

    一.简介 当查询比较复杂时,需要很多判断或者跨方法传递参数时使用 二.扩展类   public static class DynamicLinqExpressions { public static ...

  6. EntityFramework.Extended 对EF进行扩展

    前言 Entity Framework 延伸系列目录 今天我们来讲讲EntityFramework.Extended 首先科普一下这个EntityFramework.Extended是什么,如下: 这 ...

  7. EF Core扩展工具记录

    Microsoft.EntityFrameworkCore.AutoHistory Microsoft.EntityFrameworkCore 的一个插件,支持自动记录数据更改历史记录. GitHub ...

  8. 采用EntityFramework.Extended 对EF进行扩展

    今天我们来讲讲EntityFramework.Extended 首先科普一下这个EntityFramework.Extended是什么,如下: 这是一个对Entity Framework进行扩展的类库 ...

  9. EF 查询扩展

    using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Da ...

随机推荐

  1. Js script type="text/template"的使用简单说明

    <script type="text/template" id="treeTableTpl"> <tr id="{{row.id}} ...

  2. FLV格式解析(转)

    flv格式详解+实例剖析:https://www.cnblogs.com/lidabo/p/9018548.html FLV 封装格式解析:https://www.cnblogs.com/leisur ...

  3. 利用数据结构排序的priority_queue

    考虑以下几个问题: 将一个序列排序 某神仙: \(\mathtt{sort}\) !!! 每次取出最前面的两个数 某神仙: \(a_i\) 和 \(a_{i+1}\) 啊!! 相加,再加入序列 某神仙 ...

  4. 从ICG cell 在 library 中的定义说起

    如Coding 时需要考虑什么样的代码风格会使gating 的效率更高:综合时需要特别设置要插入的gating 类型,每个gating 的fanout 范围,是否可以跨层次,是否需要做physical ...

  5. css: transform导致文字显示模糊

    css: transform导致文字显示模糊 有人认为模糊的原因是:"transform时div的宽度或者高度并不是偶数,偏移 50% 之后,像素点不是整数,和显示像素没有对上". ...

  6. windows系统下,gpu开发环境部署

    1,安装python,使用anaconda或者直接用python.exe安装都可以.我用的是python3.6版的 对于相关的程序包,比如tensorflow或者opencv等,anaconda可以在 ...

  7. 高内存 高CPU 劣质网络下的测试

    内存 先把系统的虚拟内存去掉 (右键我的电脑属性里有的.选择那个无分页文件 虚拟内存在任务管理器就不显示了), 然后机子本身内存不高,开几个网页就满了       CPU cpu可以用鲁大师测试cpu ...

  8. Dictionary-Guided Editing Networks for Paraphrase Generation解读

    过程:输入->检索->编码->解码 解释:检索:输入一句话x,首先从PPDB中检索出M * 10 个释义对,并排序,记为x的本地字典: 编码:将所有的释义对编码为向量,单字转为字向量 ...

  9. mybatis--一对多关联

    今天来介绍mybatis的一对多关联 (1)首先创建数据库mybatisonetomany,并创建数据库表post和user,并向其中插入一定的数据: create database mybatiso ...

  10. Spring AOP编程(一)-AOP介绍

    1. AOP介绍 l         在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术 ...