zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    Asp.net web Api源码分析HttpServer的创建
    asp.net mvc源码分析DefaultModelBinder 集合绑定
    asp.net mvc RouteCollection的RouteExistingFiles属性理解
    Asp.net web Api源码分析HttpResponseMessage
    asp.net mvc源码分析RenderAction和RenderPartial
    AcWing 1022. 宠物小精灵之收服
    AcWing 423. 采药
    AcWing 272. 最长公共上升子序列
    算法浅谈之迭代加深
    AcWing 1023. 买书
  • 原文地址:https://www.cnblogs.com/hotss/p/3564972.html
Copyright © 2011-2022 走看看