zoukankan      html  css  js  c++  java
  • 记录一次使用 Expression 的方法

         /// <summary>
            /// 实现 Name == "a" or Name == "b" or Name == "c"
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="query"></param>
            /// <param name="condition"></param>
            /// <param name="selector"></param>
            /// <param name="values"></param>
            /// <returns></returns>
            public static IQueryable<T> WhereIfEqOr<T, TResult>(this IQueryable<T> query, bool condition, Expression<Func<T, TResult>> selector, List<TResult> values)
            {
                if (condition == false) return query;
                if (values != null && values.Count > 0)
                {
                    Type type = typeof(T);
                    ParameterExpression paramExp = Expression.Parameter(type, "x");
                    var memberExp = selector.Body as MemberExpression;
                    Expression propertyExp = Expression.Property(paramExp, memberExp.Member.Name);
                    Expression expression = Expression.Equal(propertyExp, Expression.Constant(values[0], typeof(TResult)));
                    for (int i = 1; i < values.Count; i++)
                    {
                        expression = Expression.Or(expression, Expression.Equal(propertyExp, Expression.Constant(values[i], typeof(TResult))));
                    }
                    var lambda = Expression.Lambda<Func<T, bool>>(expression, paramExp);
                    query = query.Where(lambda);
                }
                return query;
            }
         /// <summary>
            /// 实现 Name like "%a%" or Name like "%b%" or Name like "%c%"
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="query"></param>
            /// <param name="condition"></param>
            /// <param name="selector"></param>
            /// <param name="values"></param>
            /// <returns></returns>
            public static IQueryable<T> WhereIfContainsOr<T, TResult>(this IQueryable<T> query, bool condition, Expression<Func<T, TResult>> selector, List<TResult> values)
            {
                if (condition == false) return query;
                if (values != null && values.Count > 0)
                {
                    Type type = typeof(T);
                    ParameterExpression paramExp = Expression.Parameter(type, "x");
                    var memberExp = selector.Body as MemberExpression;
                    Expression propertyExp = Expression.Property(paramExp, memberExp.Member.Name);
                    Expression expression = Expression.Call(propertyExp, typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), Expression.Constant(values[0], typeof(TResult)));
                    for (int i = 1; i < values.Count; i++)
                    {
                        expression = Expression.Or(expression, Expression.Call(propertyExp, typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), Expression.Constant(values[i], typeof(TResult))));
                    }
                    var lambda = Expression.Lambda<Func<T, bool>>(expression, paramExp);
                    query = query.Where(lambda);
                }
                return query;
            }

    这两种方法可以使用一个字段的或关系

  • 相关阅读:
    自动化测试selenium教程
    Java开发.gitignore文件包含.iml,.log的看法
    基于接口设计与编程
    搭建大众点评CAT监控平台
    正确的打日志姿势
    【每天一条Linux指令-Day1】kill掉多个mysql的进程
    一道SQL面试题——表行列数据转换(表转置)
    @SuppressWarnings注解用法详解
    Spring IoC的底层技术支持——Java反射机制
    出现java.lang.NoSuchMethodError错误的原因
  • 原文地址:https://www.cnblogs.com/dazen/p/13267563.html
Copyright © 2011-2022 走看看