Exemplo n.º 1
0
        /// <summary>生成最终的Lambda表达式
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public Expression <Func <T, bool> > BuildLambda <T>()
        {
            DataTable dtFiled = cboFiled.DataSource as DataTable;

            if (dtFiled == null)
            {
                return(null);
            }

            DataTable dtOperation = cboOperation.DataSource as DataTable;

            if (dtOperation == null)
            {
                return(null);
            }
            var        param = DynamicLinq.CreateLambdaParam <T>("c");
            Expression body  = Expression.Constant(true); //初始默认一个true

            #region 处理优先级

            //获取优先级
            Dictionary <int, List <TreeGridNode> > dicNodes = GetFirstLevelNodes(tvTaskList);
            foreach (KeyValuePair <int, List <TreeGridNode> > item in dicNodes)
            {
                Expression expressAll = ConvertToFirstLevelFiltersByTreeGridView <T>(param, item.Value);
                body = item.Value[0].Cells[0].Value.ToString().Contains("并且") ? body.AndAlso(expressAll) : body.Or(expressAll);
            }

            #endregion 处理优先级

            #region 处理非优先级

            //获取非优先级别的Filter
            List <TreeGridNode> secNodes = GetSendLevelNodes(tvTaskList);
            if (secNodes.Count > 0)
            {
                List <Filter> filters = ConvertFiltersByTreeGridView(tvTaskList, secNodes);
                if (filters.Count > 0)
                {
                    foreach (var filter in filters)
                    {
                        Expression express = param.GenerateBody <T>(filter);
                        if (filter.AndOr == "并且" || filter.AndOr == string.Empty)
                        {
                            body = body.AndAlso(express); //这里可以根据需要自由组合
                        }
                        else if (filter.AndOr == "或者")
                        {
                            body = body.Or(express);
                        }
                    }
                }
            }

            #endregion 处理非优先级

            var lambda = param.GenerateTypeLambda <T>(body); //最终组成lambda
            return(lambda);
        }
Exemplo n.º 2
0
        public static IQueryable <T> Where <T>(this IQueryable <T> query, Filter[] filters)
        {
            var param = DynamicLinq.CreateLambdaParam <T>("c");
            //初始默认一个true
            Expression body = Expression.Constant(true);

            //foreach (var filter in filters)
            //{
            //    Expression express = param.GenerateBody<T>(filter);
            //    body = body.AndAlso(express); //这里可以根据需要自由组合
            //}
            body = filters.Select(filter => param.GenerateBody <T>(filter)).Aggregate(body, (current, express) => current.AndAlso(express));
            //最终组成lambda
            var lambda = param.GenerateTypeLambda <T>(body);

            return(query.Where(lambda));
        }