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. 小白学 Python 爬虫(24):2019 豆瓣电影排行

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  2. myeclipse2017配置tomcat7.0

    具体配置参考这篇博客:https://www.cnblogs.com/alibaba-inc/p/9249135.html 期间可能会碰到这样一个问题,"The server does no ...

  3. Bugku-一段Base64-Writeup

    转载请注明出处:http://www.cnblogs.com/WangAoBo/p/7207874.html bugku - 一段Base64 - Writeup 题目: 分析: 本来看到题目名字和分 ...

  4. 离线安装requests库

    离线安装需要五个 certifi-2019.9.11-py2.py3-none-any.whl chardet-3.0.4-py2.py3-none-any.whl idna-2.8-py2.py3- ...

  5. 程序员必需知道的Chrome使用技巧(入门篇)

    浏览器版本 Chrome Canary 新增一些没有经过Google工程师的测试或使用的浏览器功能版本.Chrome Dev让大多数开发人员主要使用此版本来测试对浏览器的重大版本功能版本.Chrome ...

  6. 第十篇 深入Python的dict和set(一)

  7. sqlmap使用教程(超详细)

    -u 指定目标URL (可以是http协议也可以是https协议) -d 连接数据库 --dbs 列出所有的数据库 --current-db 列出当前数据库 --tables 列出当前的表 --col ...

  8. Bugku-CTF分析篇-中国菜刀(国产神器)

    中国菜刀 国产神器

  9. bugku flag在index里

    原题内容: http://120.24.86.145:8005/post/ Mark一下这道题,前前后后弄了两个多小时,翻了一下别的博主的wp感觉还是讲的太粗了,这里总结下自己的理解: 首先打开这道题 ...

  10. Windows下MD5校验

    参考博客:https://www.cnblogs.com/liubinghong/p/9299276.html 参考博客:https://www.jianshu.com/p/1e1d56552e03 ...