Пример #1
0
        public List <Models.Device> SearchFilter(QueryInfo input)
        {
            QueryInfo  info         = new QueryInfo();
            var        getAllDevice = _deviceRepository.GetAll();
            var        ruleInputs   = input.Filter.Rule;
            var        condition    = input.Filter.Condition;
            Expression result;

            var        sorters            = input.Sorters;
            Expression containsExpression = Expression.Constant(false);
            Expression currContains;


            Expression <Func <Models.Device, bool> > whereLambdaExpression = null;
            BinaryExpression binWhereExp = null;


            ParameterExpression parExp  = Expression.Parameter(typeof(Models.Device), "x");
            Expression          findExp = null;

            foreach (var prop in input.SearchProperties)
            {
                var propExp         = Expression.Property(parExp, prop);
                var search          = input.SearchText;
                var convertedSearch = Convert.ChangeType(search, propExp.Type);
                var con             = Expression.Constant(convertedSearch);
                switch (convertedSearch)
                {
                case string _:
                    currContains       = info.GetBinaryExpressionForString("ct", propExp, con);
                    containsExpression = Expression.OrElse(containsExpression, currContains);
                    break;

                case int _:
                    currContains       = info.GetBinaryExpressionForInt("ct", propExp, con);
                    containsExpression = Expression.OrElse(containsExpression, currContains);
                    break;

                default:
                    throw new UserFriendlyException("Mrs");
                }
            }


            var filterResult = info.FilterRuleNested <Models.Device>(parExp, ruleInputs, condition);

            result = Expression.AndAlso(containsExpression, filterResult);

            //foreach (var ruleInput in ruleInputs)
            //{
            //    if (input.Filter.Condition == "and")
            //    {
            //        findExp = ConstantExpression.Constant(true);
            //        binWhereExp =
            //            info.GetWhereExp<Models.Device>(parExp, ruleInput.Operator, ruleInput.Property, ruleInput.Value);
            //        findExp = BinaryExpression.AndAlso(findExp, binWhereExp);
            //    }
            //    else if (input.Filter.Condition == "or")
            //    {
            //        findExp = Expression.Constant(false);
            //        binWhereExp =
            //            info.GetWhereExp<Models.Device>(parExp, ruleInput.Operator, ruleInput.Property,
            //                ruleInput.Value);
            //        findExp = BinaryExpression.OrElse(findExp, binWhereExp);
            //    }
            //}

            whereLambdaExpression = info.GetWhereLambda <Models.Device>(result, parExp);

            Expression <Func <Models.Device, object> > order = null;

            getAllDevice = getAllDevice.Where(whereLambdaExpression);
            var sorted = false;

            foreach (var sorter in sorters)
            {
                order = info.OrderThings <Models.Device>(sorter.Property, sorter.Direction);
                if (sorter.Direction.ToLower().Equals("asc"))
                {
                    if (!sorted)
                    {
                        getAllDevice = getAllDevice.OrderBy(order);
                        sorted       = true;
                    }
                    else
                    {
                        getAllDevice = ((IOrderedQueryable <Models.Device>)getAllDevice).ThenBy(order);
                    }
                }

                if (sorter.Direction.ToLower().Equals("desc"))
                {
                    if (!sorted)
                    {
                        getAllDevice = getAllDevice.OrderByDescending(order);
                        sorted       = true;
                    }
                    else
                    {
                        getAllDevice = ((IOrderedQueryable <Models.Device>)getAllDevice).ThenByDescending(order);
                    }
                }
            }

            return(getAllDevice.Skip(info.Skip).Take(info.Take).ToList());
        }