• Expression拼接


    public static  class ExpressionHelp
        {
            /// <summary>
            /// Lambda表达式拼接
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="first"></param>
            /// <param name="second"></param>
            /// <param name="merge"></param>
            /// <returns></returns>
            public static Expression<T> Compose<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>
            /// <typeparam name="T"></typeparam>
            /// <param name="first"></param>
            /// <param name="second"></param>
            /// <returns></returns>
            public static Expression<Func<T1, T2, bool>> And<T1, T2>(this Expression<Func<T1, T2, bool>> first, Expression<Func<T1, T2, bool>> second)
            {
                return first.Compose(second, Expression.And);
            }
            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);
            }
    
            /// <summary>
            /// or扩展
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="first"></param>
            /// <param name="second"></param>
            /// <returns></returns>
            public static Expression<Func<T1, T2, bool>> Or<T1, T2>(this Expression<Func<T1, T2, bool>> first, Expression<Func<T1, T2, bool>> second)
            {
                return first.Compose(second, Expression.Or);
            }
            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);
            }
    
        }
  • 相关阅读:
    6.3 The Memory Hierarchy
    去掉正在打印对话框
    SQL SERVER格式化字符串位数,不足补零
    WCF学习系列二_使用IIS发布WCF服务
    WCF学习系列一_创建第一个WCF服务
    IrisSkin4控件使用方法
    窗体加载后的设置焦点事件
    SQL 分组后取最小行号记录
    二维数组、齿形数组和游长变元表
    C#数组按值和按引用传递数组区别
  • 原文地址:https://www.cnblogs.com/330774495qq/p/12855727.html
Copyright © 2020-2023  润新知