表达式树 动态 where select


        #region 私有函数

        //Expression表达式 包含字符串
        private static Expression<Func<T, bool>> GetConditionExpression<T>(string[] options, string fieldName)
        {
            ParameterExpression left = Expression.Parameter(typeof(T), "c");//c=>
            Expression expression = Expression.Constant(false);
            foreach (var optionName in options)
            {
                Expression right = Expression.Call
                       (
                          Expression.Property(left, typeof(T).GetProperty(fieldName)),  //c.DataSourceName
                          typeof(string).GetMethod("Contains", new Type[] { typeof(string) }),// 反射使用.Contains()方法                         

                         Expression.Constant(optionName)           // .Contains(optionName)
                       );
                expression = Expression.Or(right, expression);//c.DataSourceName.contain("") || c.DataSourceName.contain("")
            }
            Expression<Func<T, bool>> finalExpression
                = Expression.Lambda<Func<T, bool>>(expression, new ParameterExpression[] { left });
            return finalExpression;
        }

        //Expression表达式 selector (rec) => new ParadigmSearchListData { Number = rec.NUMBER, Name = rec.RES_NAME };
        //参数为 源数据(TSource)的字段
        private static Expression<Func<TSource, TResult>> CreateSelecter<TSource, TResult>(string colNumber, string colName)
        {
            Expression<Func<TSource, TResult>> selector = null;

            //(rec)
            ParameterExpression param = Expression.Parameter(typeof(TSource), "rec");
            //new ParadigmSearchListData
            var v0 = Expression.New(typeof(TResult));
            //Number
            var v1 = typeof(TResult).GetProperty("Number");
            //rec.NUMBER
            var v2 = Expression.Property(param, typeof(TSource).GetProperty(colNumber));
            //Name
            var v3 = typeof(TResult).GetProperty("Name");
            //rec.RES_NAME
            var v4 = Expression.Property(param, typeof(TSource).GetProperty(colName));

            Expression body = Expression.MemberInit(v0,
                //{ Number = rec.NUMBER, Name = rec.RES_NAME }
                new MemberBinding[]
                {
                    //Number = rec.NUMBER
                    Expression.Bind(v1, v2),
                    //Name = rec.RES_NAME
                    Expression.Bind(v3, v4)
                });

            selector = (Expression<Func<TSource, TResult>>)Expression.Lambda(body, param);

            return selector;
        }

        //select 参数colName为 源数据(TSource)的字段
        private static IQueryable<TResult> GetListDataSelect<TSouce, TResult>(IQueryable<TSouce> entTable, params string[] colName)
        {
            //依据IQueryable数据源构造一个查询
            IQueryable<TSouce> custs = entTable;
            //组建一个表达式树来创建一个参数
            Expression selector = null;
            if (colName.Count() == 2)
            {
                selector = CreateSelecter<TSouce, TResult>(colName[0], colName[1]);
            }

            //组建表达式树:Select(c=>c.ContactName)
            Expression expr = Expression.Call(typeof(Queryable), "Select",
                new Type[] { typeof(TSouce), typeof(TResult) },
                Expression.Constant(custs), selector);
            //使用表达式树来生成动态查询
            IQueryable<TResult> query = entTable.AsQueryable()
                .Provider.CreateQuery<TResult>(expr);

            return query;
        }
        //where
        private static IQueryable<TSouce> GetListDataWhere<TSouce, TResult>(
            IQueryable<TSouce> entTable, string[] options, string fieldName)
        {
            //包含
            Expression whereor = GetConditionExpression<TSouce>(options, fieldName);

            Expression expr = Expression.Call(typeof(Queryable), "Where",
                new Type[] { typeof(TSouce) },
                Expression.Constant(entTable), whereor);
            //生成动态查询
            IQueryable<TSouce> query = entTable.AsQueryable()
                .Provider.CreateQuery<TSouce>(expr);

            return query;
        }


        //获得数据 name
        static private IQueryable<TResult> GetListDataName<TSouce, TResult>(IQueryable<TSouce> entTable,
            string wordKey, string whereColName, params string[] selectColName)
        {
            return GetListDataSelect<TSouce, TResult>(
                    GetListDataWhere<TSouce, TResult>(entTable, new string[] { wordKey }, whereColName),
                    selectColName
                    );
        }

        #endregion


        //获得数据 材料与器械 Name
        static public List<ParadigmSearchListData> GetListDataMaterialApparatusName(string wordKey)
        {
            List<ParadigmSearchListData> listData = null;
                        
            using (ParadigmSearch.ParadigmSearchEEntities ent = new ParadigmSearch.ParadigmSearchEEntities())
            {
                listData = GetListDataName<ParadigmSearch.T_MATERIAL_APPARATUS, ParadigmSearchListData>(
                    ent.T_MATERIAL_APPARATUS, wordKey, "RES_NAME", "NUMBER", "RES_NAME").ToList();             
            }

            return listData;
        }



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值