コード例 #1
0
        private static bool WhereQuery(WhereClause where, IDictionary<string, object> e)
        {
            var result = false;

            var obj = e[where.Property];

            var value = ParseType(obj);

            if (where.Type == typeof(int))
            {
                var n = (int)where.Value;

                var number = value.Type == typeof(int) ? (int)value.Value : Convert.ToInt32(value.Value);

                switch (where.Operator)
                {
                    case WhereOperator.Equals:
                        result = (number == n);
                        break;

                    case WhereOperator.GreaterThan:
                        result = (number > n);
                        break;
                    case WhereOperator.LessThan:
                        result = (number < n);
                        break;

                    case WhereOperator.Contains:
                    case WhereOperator.EndsWith:
                    case WhereOperator.StartsWith:
                        throw new InvalidOperationException();
                }
            }
            else
            {
                var s = (string)where.Value;

                var str = Convert.ToString(value.Value);

                switch (where.Operator)
                {
                    case WhereOperator.Contains:
                        result = -1 != str.IndexOf(s);
                        break;

                    case WhereOperator.Equals:
                        result = (str == s);
                        break;

                    case WhereOperator.EndsWith:
                        result = str.EndsWith(s);
                        break;

                    case WhereOperator.StartsWith:
                        result = str.StartsWith(s);
                        break;

                    case WhereOperator.GreaterThan:
                        result = (0 < str.CompareTo(s));
                        break;

                    case WhereOperator.LessThan:
                        result = (0 > str.CompareTo(s));
                        break;
                }
            }

            return result;
        }
コード例 #2
0
        private static WhereClause ParseWhere(string[] tokens, ref int i)
        {
            var where = new WhereClause();

            ++i;

            where.Property = tokens[i];

            ++i;

            var op = tokens[i].ToLowerInvariant();

            switch (op)
            {
                case "contains":
                    where.Operator = WhereOperator.Contains;
                    break;

                case "eq":
                case "equals":
                    where.Operator = WhereOperator.Equals;
                    break;

                case "gt":
                case "greaterthan":
                    where.Operator = WhereOperator.GreaterThan;
                    break;

                case "lt":
                case "lessthan":
                    where.Operator = WhereOperator.LessThan;
                    break;

                case "endswith":
                    where.Operator = WhereOperator.EndsWith;
                    break;

                case "startswith":
                    where.Operator = WhereOperator.StartsWith;
                    break;
            }

            ++i;

            var type = ParseType(tokens[i]);

            where.Type = type.Type;

            where.Value = type.Value;

            return where;
        }