• C# 反射 表达式树 模糊搜索


    反射实体T,非datetime字段反射获取表达式树

      public static Expression<Func<T, bool>> GetSearchExpression<T>(string SearchString)
            {
                Expression<Func<T, bool>> filter = null;
                if (string.IsNullOrEmpty(SearchString)) return null;

                var left = Expression.Parameter(typeof(T), "m");
                Expression expression = Expression.Constant(false);

                T obj = default(T);
                var type = typeof(T);
                obj = (T)Activator.CreateInstance(type);
                var propertyInfos = type.GetProperties();
                foreach (var propertyInfo in propertyInfos)
                {
                    if (propertyInfo.Name.ToLower() == "id" || propertyInfo.PropertyType == typeof(DateTime)) continue;

                    Expression tostring = Expression.Call
             (
                 Expression.Property(left, typeof(T).GetProperty(propertyInfo.Name).Name),
               typeof(object).GetMethod("ToString"new Type[] { })
             );

                    Expression right = Expression.Call
                          (
                              tostring,
                            typeof(string).GetMethod("Contains"new Type[] { typeof(string) }),
                            Expression.Constant(SearchString)
                          );

                    expression = Expression.Or(right, expression);
                }
                filter = Expression.Lambda<Func<T, bool>>(expression, new[] { left });
                return filter;
            }
  • 相关阅读:
    codeforces 407B Long Path
    CodeForces 489C Given Length and Sum of Digits...
    hacker cup 2015 Round 1 解题报告
    hacker cup 2015 资格赛
    Codeforces 486(#277 Div 2) 解题报告
    POJ 3468 A Simple Problem with Integers splay
    Codeforces 484(#276 Div 1) D Kindergarten DP
    求平均值问题201308031210.txt
    I love this game201308022009.txt
    QQ
  • 原文地址:https://www.cnblogs.com/hotss/p/3564972.html
Copyright © 2020-2023  润新知