zoukankan      html  css  js  c++  java
  • EF Expression 扩展

    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("2"));
    expr = expr.Or(n => n.PID > 5);
    context.表名.Where(expr).ToList()
    //或者(针对自定义对象)
    list = list.Where(expr.Compile()).ToList();
  • 相关阅读:
    教你如何在Drcom下使用路由器上校园网(以广东工业大学、极路由1S HC5661A为例)
    selenium跳过webdriver检测并模拟登录淘宝
    无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口
    使用xib制作界面有时会出现button无法点击,解决办法
    1.开博客的第一篇
    6.关于瀑布模型
    3.Freshman阶段学习内容的确定
    7.Solution的Build、Rebuild和Clean
    4.词法结构JavaScript权威指南笔记
    8.对于.NET的初步理解和介绍
  • 原文地址:https://www.cnblogs.com/jasonlai2016/p/10842454.html
Copyright © 2011-2022 走看看