Ejemplo n.º 1
0
        // -------EXPRESSION SAMPLE------- //
        public IQueryable <Usage> QueryInfo(QueryInfo input)
        {
            var obj = new QueryInfo();

            var query = _context.Usages.Include(p => p.Person).Include(d => d.Device).AsQueryable();

            var rules     = input.Filter.Rules;
            var sorters   = input.Sorters;
            var condition = input.Filter.Condition;
            var skipNum   = input.Skip;
            var takeNum   = input.Take > 0 ? input.Take : 1;

            ParameterExpression parameterEx = Expression.Parameter(typeof(Usage), "x");
            Expression          result;

            if (condition == "and")
            {
                Expression <Func <Usage, bool> > trueExp = x => true;
                result = trueExp.Body;
            }
            else
            {
                Expression <Func <Usage, bool> > falseExp = x => false;
                result = falseExp.Body;
            }

            foreach (var rule in rules)
            {
                var property         = rule.Property;
                var binOperator      = rule.Operator;
                var value            = rule.Value;
                var binaryExpression = obj.GetBinaryExpression(parameterEx, binOperator, property, value);

                switch (condition)
                {
                case "and":
                    result = Expression.AndAlso(result, binaryExpression);
                    break;

                case "or":
                    result = Expression.OrElse(result, binaryExpression);
                    break;

                default:
                    throw new InvalidOperationException();
                }
            }

            var whereEx = obj.GetWhere <Usage>(result, parameterEx);

            query = query.Where(whereEx);

            bool sortedFirst = false;

            foreach (var sort in sorters)
            {
                var sortProperty  = sort.Property;
                var sortDirection = sort.SortDirection;

                switch (sortDirection)
                {
                case "asc":
                    if (!sortedFirst)
                    {
                        query       = query.OrderBy(obj.GetOrderByExpression <Usage>(sortProperty));
                        sortedFirst = true;
                    }
                    else
                    {
                        query = ((IOrderedQueryable <Usage>)query).ThenBy(obj.GetOrderByExpression <Usage>(sortProperty));
                    }
                    break;

                case "desc":
                    if (!sortedFirst)
                    {
                        query       = query.OrderByDescending(obj.GetOrderByExpression <Usage>(sortProperty));
                        sortedFirst = true;
                    }
                    else
                    {
                        query = ((IOrderedQueryable <Usage>)query).ThenByDescending(obj.GetOrderByExpression <Usage>(sortProperty));
                    }
                    break;

                default:
                    throw new InvalidOperationException();
                }
            }

            query = query.Skip(skipNum).Take(takeNum);
            return(query);

            //var op = input.Filter.Rules[0].Operator;
            //var prop = input.Filter.Rules[0].Property;
            //var src = input.Filter.Rules[0].Value;
            //var ob = input.Sorters[0].Property;

            //var qu = new QueryInfo();

            //var whereExFilter = qu.GetWhereExpression<Usage>(op, prop, src);

            //var orderByFilter = qu.GetOrderByExpression<Usage>(ob);

            //query = query.OrderBy(x => x.UsedFrom);

            //query = ((IOrderedQueryable<Usage>) query).ThenBy(x => x.Id);


            //return query.Where(whereExFilter).OrderBy(orderByFilter);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Search
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        //[AbpAuthorize(PermissionNames.GlobalConfiguration_Users_Read)]
        public List <DeviceDto> QueryInfoSearch(QueryInfo input)
        {
            var        obj                = new QueryInfo();
            var        skipNum            = input.Skip;
            var        takeNum            = input.Take;
            var        query              = _deviceRepository.GetAll();
            var        sorters            = input.Sorters;
            var        rules              = input.Filter.Rules;
            var        condition          = input.Filter.Condition;
            var        parameterEx        = Expression.Parameter(typeof(Device), "x");
            Expression containsExpression = Expression.Constant(false);
            Expression currentContainsExpression;
            Expression result;

            foreach (var property in input.SearchProperties)
            {
                currentContainsExpression = obj.GetBinaryExpression(parameterEx, "ct", property, input.SearchText);
                containsExpression        = Expression.OrElse(containsExpression, currentContainsExpression);
            }

            result = obj.GetFilteredList <Device>(parameterEx, rules, condition);
            result = Expression.AndAlso(containsExpression, result);

            var whereEx = obj.GetWhere <Device>(result, parameterEx);

            query = query.Where(whereEx);

            var sortedFirst = false;

            foreach (var sort in sorters)
            {
                var sortProperty  = sort.Property;
                var sortDirection = sort.Direction;

                switch (sortDirection)
                {
                case "asc":
                    if (!sortedFirst)
                    {
                        query       = query.OrderBy(obj.GetOrderByExpression <Device>(sortProperty));
                        sortedFirst = true;
                    }
                    else
                    {
                        query = ((IOrderedQueryable <Device>)query).ThenBy(
                            obj.GetOrderByExpression <Device>(sortProperty));
                    }

                    break;

                case "desc":
                    if (!sortedFirst)
                    {
                        query       = query.OrderByDescending(obj.GetOrderByExpression <Device>(sortProperty));
                        sortedFirst = true;
                    }
                    else
                    {
                        query = ((IOrderedQueryable <Device>)query).ThenByDescending(
                            obj.GetOrderByExpression <Device>(sortProperty));
                    }

                    break;

                default:
                    throw new InvalidOperationException();
                }
            }

            query = query.Skip(skipNum).Take(takeNum);
            var queryToReturn = ObjectMapper.Map <List <DeviceDto> >(query);

            return(queryToReturn.ToList());
        }
Ejemplo n.º 3
0
        public IQueryable QueryInfooo([FromBody] QueryInfo input)
        {
            QueryInfo obj        = new QueryInfo();
            var       result     = _context.UredjajUzetVraceni.AsQueryable();
            var       ruleInputs = input.Filter.Rules;

            Expression <Func <UredjajUzetVracen, bool> > whereLaqmbdaExpression = null;
            BinaryExpression binWhereExp = null;

            //var filterCondition = input.Filter.Condition;
            ParameterExpression parExp  = Expression.Parameter(typeof(UredjajUzetVracen), "x");
            Expression          findExp = null;

            foreach (var ruleInput in ruleInputs)
            {
                if (input.Filter.Condition == "and")
                {
                    findExp     = Expression.Constant(true);
                    binWhereExp =
                        obj.GetBinary <UredjajUzetVracen>(parExp, ruleInput.Operator, ruleInput.Property, ruleInput.Value);
                    findExp = Expression.AndAlso(findExp, binWhereExp);
                }
                else if (input.Filter.Condition == "or")
                {
                    findExp     = Expression.Constant(false);
                    binWhereExp =
                        obj.GetBinary <UredjajUzetVracen>(parExp, ruleInput.Operator, ruleInput.Property, ruleInput.Value);
                    findExp = Expression.OrElse(findExp, binWhereExp);
                }
            }

            whereLaqmbdaExpression = obj.GetWhere <UredjajUzetVracen>(findExp, parExp);

            Expression <Func <UredjajUzetVracen, object> > order = null;

            result = result.Where(whereLaqmbdaExpression);
            var  sorters   = input.Sorters;
            bool condition = false;

            foreach (var sorter in sorters)
            {
                order = obj.GetOrderBy <UredjajUzetVracen>(sorter.Property, sorter.SortDirection);

                if (sorter.SortDirection.ToLower().Equals("asc"))
                {
                    if (!condition)
                    {
                        result    = result.OrderBy(order);
                        condition = true;
                    }
                    else
                    {
                        result = ((IOrderedQueryable <UredjajUzetVracen>)result).ThenBy(order);
                    }
                }
                if (sorter.SortDirection.ToLower().Equals("desc"))
                {
                    if (!condition)
                    {
                        result    = result.OrderByDescending(order);
                        condition = true;
                    }
                    else
                    {
                        result = ((IOrderedQueryable <UredjajUzetVracen>)result).ThenByDescending(order);
                    }
                }
            }

            result = result.Skip(input.Skip).Take(input.Take);

            return(result);
        }