首页 > 编程语言 >C# 好用的Expression动态拼接帮助类

C# 好用的Expression动态拼接帮助类

时间:2024-04-15 10:33:35浏览次数:24  
标签:return C# public second static Expression 好用 first

public static class ExpressionHandle
{
    /// <summary>
    /// Linq的And操作
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="first"></param>
    /// <param name="second"></param>
    /// <param name="tf">是否拼接param>
    /// <returns>val为空就返回first,否则返回first与second的And操作</returns>
    public static Expression<Func<T, bool>> IfAnd<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second, bool tf)
    {
        if (tf)
        {
            return first.ExpressionAnd(second);
        }
        return first;
    }

    /// <summary>
    /// Linq的Or操作
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="first"></param>
    /// <param name="second"></param>
    /// <param name="tf">是否为拼接</param>
    /// <returns>val为空就返回first,否则返回first与second的Or操作</returns>
    public static Expression<Func<T, bool>> IfOr<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second, bool tf)
    {
        if (tf)
        {
            return first.ExpressionOr(second);
        }
        return first;
    }

    /// <summary>
    /// Linq的And操作
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="first"></param>
    /// <param name="second"></param>
    /// <param name="tf">是否拼接param>
    /// <returns>val为空就返回first,否则返回first与second的And操作</returns>
    public static Expression<Func<T1, T2, bool>> IfAnd<T1, T2>(this Expression<Func<T1, T2, bool>> first, Expression<Func<T1, T2, bool>> second, bool tf)
    {
        if (tf)
        {
            return first.ExpressionAnd(second);
        }
        return first;
    }

    /// <summary>
    /// Linq的Or操作
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="first"></param>
    /// <param name="second"></param>
    /// <param name="tf">是否为拼接</param>
    /// <returns>val为空就返回first,否则返回first与second的Or操作</returns>
    public static Expression<Func<T1, T2, bool>> IfOr<T1, T2>(this Expression<Func<T1, T2, bool>> first, Expression<Func<T1, T2, bool>> second, bool tf)
    {
        if (tf)
        {
            return first.ExpressionOr(second);
        }
        return first;
    }

    /// <summary>
    /// Linq的And操作
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="first"></param>
    /// <param name="second"></param>
    /// <param name="tf">是否拼接param>
    /// <returns>val为空就返回first,否则返回first与second的And操作</returns>
    public static Expression<Func<T1, T2, T3, bool>> IfAnd<T1, T2, T3>(this Expression<Func<T1, T2, T3, bool>> first, Expression<Func<T1, T2, T3, bool>> second, bool tf)
    {
        if (tf)
        {
            return first.ExpressionAnd(second);
        }
        return first;
    }

    /// <summary>
    /// Linq的Or操作
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="first"></param>
    /// <param name="second"></param>
    /// <param name="tf">是否为拼接</param>
    /// <returns>val为空就返回first,否则返回first与second的Or操作</returns>
    public static Expression<Func<T1, T2, T3, bool>> IfOr<T1, T2, T3>(this Expression<Func<T1, T2, T3, bool>> first, Expression<Func<T1, T2, T3, bool>> second, bool tf)
    {
        if (tf)
        {
            return first.ExpressionOr(second);
        }
        return first;
    }
}

  

public static class ExpressionHelper
{
    private static Expression<T> Combine<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
    {
        var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f);
        var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
        return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
    }
    /// <summary>
    /// And合并
    /// </summary>
    /// <returns></returns>
    public static Expression<Func<T, bool>> ExpressionAnd<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
    {
        return first.Combine(second, Expression.And);
    }
    /// <summary>
    /// OR合并
    /// </summary>
    /// <returns></returns>
    public static Expression<Func<T, bool>> ExpressionOr<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
    {
        return first.Combine(second, Expression.Or);
    }
    /// <summary>
    /// And合并
    /// </summary>
    /// <returns></returns>
    public static Expression<Func<T1, T2, bool>> ExpressionAnd<T1, T2>(this Expression<Func<T1, T2, bool>> first, Expression<Func<T1, T2, bool>> second)
    {
        return first.Combine(second, Expression.And);
    }
    /// <summary>
    /// OR合并
    /// </summary>
    /// <returns></returns>
    public static Expression<Func<T1, T2, bool>> ExpressionOr<T1, T2>(this Expression<Func<T1, T2, bool>> first, Expression<Func<T1, T2, bool>> second)
    {
        return first.Combine(second, Expression.Or);
    }
    /// <summary>
    /// And合并
    /// </summary>
    /// <returns></returns>
    public static Expression<Func<T1, T2, T3, bool>> ExpressionAnd<T1, T2, T3>(this Expression<Func<T1, T2, T3, bool>> first, Expression<Func<T1, T2, T3, bool>> second)
    {
        return first.Combine(second, Expression.And);
    }
    /// <summary>
    /// OR合并
    /// </summary>
    /// <returns></returns>
    public static Expression<Func<T1, T2, T3, bool>> ExpressionOr<T1, T2, T3>(this Expression<Func<T1, T2, T3, bool>> first, Expression<Func<T1, T2, T3, bool>> second)
    {
        return first.Combine(second, Expression.Or);
    }
}

  

public class ParameterRebinder : ExpressionVisitor
{
    private readonly Dictionary<ParameterExpression, ParameterExpression> map;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="map"></param>
    public ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
    {
        this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="map"></param>
    /// <param name="exp"></param>
    /// <returns></returns>
    public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
    {
        return new ParameterRebinder(map).Visit(exp);
    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="p"></param>
    /// <returns></returns>
    protected override Expression VisitParameter(ParameterExpression p)
    {
        ParameterExpression replacement;
        if (map.TryGetValue(p, out replacement))
        {
            p = replacement;
        }
        return base.VisitParameter(p);
    }

}

  

标签:return,C#,public,second,static,Expression,好用,first
From: https://www.cnblogs.com/wang920607/p/18135340

相关文章

  • day01-03_我的Java学习笔记(Java基础语法--注释、字面量、变量、二进制、ASCII编码、
    1.Java基础语法1.1注释1.2字面量(Python中叫数据类型)1.3变量1.3.1变量的定义及使用1.3.2变量使用注意事项1.4数据的存储形式:二进制字节、字、bit、byte的关系:字word字节byte位bit,来自英文bit,音译为“比特”,表示二进制位。字长是指字的......
  • async与await暂停作用
    1.asyncfunctionRequest(){awaitnewPromise(res=>{setTimeout(()=>{console.log(1)res()},1000)})console.log(4);newPromise(res=>{setTimeout(()=>{......
  • hashchang事件是异步更新的
    1.代码//此时会触发hashchangelocation.hash='/test'window.addEventListener('hashchange',()=>{console.log('hashchenge')})//此时不会触发hashchangelocation.hash='/test'setTimeOut(()......
  • vscode使用ES6装饰器器语法
    1.react项目配置package.json需要安装npmi@babel/plugin-proposal-decorators可能需要重启项目{"babel":{"plugins":[["@babel/plugin-proposal-decorators",{"legacy":true}......
  • vscode使用npm安装依赖报错
    1.报错信息npmERR!codeEPERMnpmERR!syscallopennpmERR!pathC:\Node\node_cache_cacache\index-v5\4b\d5\ac02d8eac51f5c368b18a948d14b7ba882d43e6f4cafddbf9dda740d2afanpmERR!errno-4048npmERR!Error:EPERM:operationnotpermitted,open'C:......
  • Docker从入门到精通:Docker镜像相关命令学习
    简介在Docker中,管理镜像(Images)是非常重要的,因为镜像是容器的基础。本文将介绍一些常用的Docker镜像管理命令,帮助你有效地管理和操作Docker镜像。dockerimages查看本地已有的镜像列表,可以使用dockerimages命令:dockerimages-REPOSITORY镜像的仓库源-TAG镜......
  • Docker从入门到精通:Docker pull命令学习
    Docker是一种流行的容器化平台,它允许用户构建、分享和运行容器化的应用程序。要使用Docker,您需要先下载所需的Docker镜像。之前我们介绍了在Ubuntu系统上安装docker,本文将接着介绍如何使用DockerPull命令下载Docker镜像的步骤。dockerpulldockerpull命令是用于从镜像仓库中......
  • lCS(最长公共子串)
    之前一直写的最长公共子序列,从来没写过最长公共子串这个算法,也因为这个,在今年的蓝桥杯省赛中有个题目用的暴力字符串匹配,导致了丢分(也可能拿不到省一了,哎)其实就是一个二维的dp,dp[i][j]表示第一个以i结尾,第二个以j结尾的最长长度。1初始化,第一个串的下标按行存储,第二个串的下标......
  • cka刷题2024
    一.基于角色的访问控制-RBAC(4/25%)参考:https://kubernetes.io/zh-cn/docs/reference/access-authn-authz/rbac/Kubectlcreateclusterroledeployment-clusterrole--verb=create--resource=deployments,statefulsets,daemonsetsekubectlcreateserviceaccountcicd-tok......
  • C#开发的PhotoNet看图软件 - 开源研究系列文章 - 个人小作品
          这几天忙于编程。上次发布了壁纸管理器的插件版(https://www.cnblogs.com/lzhdim/p/18074135),然后整理和添加了一下相关的壁纸图片文件,虽然在管理器中也能浏览壁纸并设置,但是还是看图软件更加方便,双击图片文件就能打开浏览了。笔者用的Windows11操作系统,也自带了图......