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;
            }
  • 相关阅读:
    nyoj 164&amp;&amp;poj2084 Game of Connections 【卡特兰】
    1、Cocos2dx 3.0游戏开发找小三之前言篇
    hibernate一级缓存,二级缓存和查询缓存
    WCF服务端调用client.
    优化中的subgradient方法
    Xsolla和Crytek合作,对游戏战争前线推出全新支付方式
    CNZZ站点流量统计原理简析
    分数加减法
    【jvm】windows下查看java进程下多线程的相关信息
    【面试 docker+k8s+jenkins】【第二十篇】docker+k8s+jenkins相关面试
  • 原文地址:https://www.cnblogs.com/hotss/p/3564972.html
Copyright © 2011-2022 走看看