Esempio n. 1
0
        SearchExpression ToPascal(SearchExpression expression)
        {
            SearchOption <T> newSearchOption = new SearchOption <T>();

            if (expression is PropertySearchExpression)
            {
                var propertyExpressionSearch = expression as PropertySearchExpression;
                var asList = propertyExpressionSearch.Property.Split('_').ToList();
                asList = asList.Select(a => a.First().ToString().ToUpper() + a.Substring(1, a.Length - 1)).ToList();
                newSearchOption.Expression = new PropertySearchExpression()
                {
                    Property = string.Join("", asList),
                    Operator = propertyExpressionSearch.Operator,
                    Value    = propertyExpressionSearch.Value
                };
            }

            if (expression is BinarySearchExpression <T> )
            {
                BinarySearchExpression <T> binarySearchExpression = expression as BinarySearchExpression <T>;
                newSearchOption.Expression = new BinarySearchExpression <T>();
                (newSearchOption.Expression as BinarySearchExpression <T>).LeftSearch     = ToPascal(binarySearchExpression.LeftSearch);
                (newSearchOption.Expression as BinarySearchExpression <T>).BinaryOperator = binarySearchExpression.BinaryOperator;
                (newSearchOption.Expression as BinarySearchExpression <T>).RightSearch    = ToPascal(binarySearchExpression.RightSearch);
            }
            return(newSearchOption.Expression);
        }
Esempio n. 2
0
        public static SearchExpression DeserializeSearchExpression(string searchExpresssion)
        {
            if (string.IsNullOrEmpty(searchExpresssion))
            {
                return(null);
            }
            Stack <Token> result    = GetExpressionTree(searchExpresssion);
            Stack <Token> tokenList = new Stack <Token>();

            foreach (Token t in result)
            {
                tokenList.Push(t);
            }
            SearchExpression expression = GetSearchExpression(tokenList);

            return(expression);
        }
Esempio n. 3
0
        //Match function built for repository that doesn't have the power of search by default. Meaning search will be done after results are received. Example of this storage is Azure TableStorage
        bool Match(T data, SearchExpression expression)
        {
            if (expression == null)
            {
                return(true);
            }
            if (expression is PropertySearchExpression)
            {
                int      tempInt    = 0;
                double   tempDouble = 0;
                DateTime tempDatetime;
                PropertySearchExpression propertySearchExpression = expression as PropertySearchExpression;
                PropertyInfo             pInfo = typeof(T).GetProperty(propertySearchExpression.Property);
                var value = pInfo.GetValue(data);
                switch (propertySearchExpression.Operator)
                {
                case "=": return((value ?? "").ToString().Trim().ToLower() == propertySearchExpression.Value.Trim().ToLower());

                case "!=": return((value ?? "").ToString().Trim().ToLower() != propertySearchExpression.Value.Trim().ToLower());

                case ">":
                    if (pInfo.GetType() == typeof(int) && int.TryParse(propertySearchExpression.Value, out tempInt))
                    {
                        return((int)value > tempInt);
                    }
                    if (pInfo.GetType() == typeof(double) && double.TryParse(propertySearchExpression.Value, out tempDouble))
                    {
                        return((double)value > tempDouble);
                    }
                    if (pInfo.GetType() == typeof(DateTime) && DateTime.TryParse(propertySearchExpression.Value, out tempDatetime))
                    {
                        return((DateTime)value > tempDatetime);
                    }

                    else
                    {
                        return(false);
                    }

                case "<":
                    if (pInfo.GetType() == typeof(int) && int.TryParse(propertySearchExpression.Value, out tempInt))
                    {
                        return((int)value < tempInt);
                    }
                    if (pInfo.GetType() == typeof(double) && double.TryParse(propertySearchExpression.Value, out tempDouble))
                    {
                        return((double)value < tempDouble);
                    }
                    if (pInfo.GetType() == typeof(DateTime) && DateTime.TryParse(propertySearchExpression.Value, out tempDatetime))
                    {
                        return((DateTime)value < tempDatetime);
                    }
                    else
                    {
                        return(false);
                    }

                case "%": return((value ?? "").ToString().Trim().ToLower().Contains(propertySearchExpression.Value.Trim().ToLower()));

                default: return(false);
                }
            }

            else
            {
                BinarySearchExpression <T> binarySearchExpression = expression as BinarySearchExpression <T>;
                switch (binarySearchExpression.BinaryOperator)
                {
                case "||": return(Match(data, binarySearchExpression.LeftSearch) || Match(data, binarySearchExpression.RightSearch));

                case "&&": return(Match(data, binarySearchExpression.LeftSearch) && Match(data, binarySearchExpression.RightSearch));

                default: return(false);
                }
            }
        }