public FilterClause(string filterString)
        {
            if (filterString == null)
            {
                filterString = string.Empty;
            }

            var match = StartsWithRegex.Match(filterString);

            if (match.Success)
            {
                // startswith(field-name, 'value')
                this.Predicate = (v) => v.StartsWith(match.Groups[2].Value);
            }
            else
            {
                match = ContainsRegex.Match(filterString);
                if (match.Success)
                {
                    // contains(field-name, 'value')
                    this.Predicate = (v) => v.Contains(match.Groups[2].Value);
                }
                else
                {
                    match = EqRegex.Match(filterString);
                    if (match.Success)
                    {
                        // field-name eq 'value'
                        string value = match.Groups[2].Value;
                        this.Predicate = (v) =>
                        {
                            return(value == "null" ? string.IsNullOrEmpty(v) : v == value);
                        };
                    }
                }
            }

            if (this.Predicate != null)
            {
                this.FieldName = match.Groups[1].Value;
            }
        }
Esempio n. 2
0
        public FilterClause(string filterString)
        {
            if (filterString == null)
            {
                filterString = string.Empty;
            }

            var match = StartsWithRegex.Match(filterString);

            if (match.Success)
            {
                // startswith(field-name, 'value') eq true|false

                bool result = true;
                if (match.Groups.Count > 4)
                {
                    result = match.Groups[4].Value != "false";
                }
                string arg = match.Groups[2].Value;

                this.Predicate = (v) => v.StartsWith(arg) == result;
            }
            else
            {
                match = ContainsRegex.Match(filterString);
                if (match.Success)
                {
                    // contains(field-name, 'value') eq true|false

                    bool result = true;
                    if (match.Groups.Count > 4)
                    {
                        result = match.Groups[4].Value != "false";
                    }
                    string arg = match.Groups[2].Value;

                    this.Predicate = (v) => v.Contains(arg) == result;
                }
                else
                {
                    match = EqRegex.Match(filterString);
                    if (match.Success)
                    {
                        // field-name eq|ne 'value'
                        string value = match.Groups[3].Value;
                        string op    = match.Groups[2].Value;

                        this.Predicate = (v) =>
                        {
                            bool res = value == "null" ? string.IsNullOrEmpty(v) : v == value;

                            return(op == "ne" ? !res : res);
                        };
                    }
                }
            }

            if (this.Predicate != null)
            {
                this.FieldName = match.Groups[1].Value;
            }
        }
        private void ExtractPredicate(string filterString)
        {
            // startswith(field-name, 'value') eq true|false
            var match = StartsWithRegex.Match(filterString);

            if (match.Success)
            {
                bool result = true;
                if (match.Groups.Count > 4)
                {
                    result = match.Groups[4].Value != "false";
                }
                string arg = match.Groups[2].Value;

                this.Predicate = (v) => v.StartsWith(arg) == result;
            }
            // contains(field-name, 'value') eq true|false
            else if ((match = ContainsRegex.Match(filterString)).Success)
            {
                bool result = true;
                if (match.Groups.Count > 4)
                {
                    result = match.Groups[4].Value != "false";
                }
                string arg = match.Groups[2].Value;

                this.Predicate = (v) => v.Contains(arg) == result;
            }
            // field-name eq|ne 'value'
            else if ((match = EqRegex.Match(filterString)).Success)
            {
                string value = match.Groups[3].Value;
                string op    = match.Groups[2].Value;

                this.Predicate = (v) =>
                {
                    bool res = value == "null" ? string.IsNullOrEmpty(v) : v == value;
                    return(op == "ne" ? !res : res);
                };
            }
            // field-name in ('value1','value2','value3')
            else if ((match = InRegex.Match(filterString)).Success)
            {
                string value = match.Groups[2].Value.Trim();

                string[] values;
                if (value.StartsWith("'"))
                {
                    values = LazyQuotesRegex.Matches(value)
                             .Select(m => m.Groups[1].Value)
                             .ToArray();
                }
                else
                {
                    values = match.Groups[2].Value
                             .Split(',').Where(s => !string.IsNullOrEmpty(s))
                             .Select(s => s.Trim(' ', '\''))
                             .ToArray();
                }

                if ((match.Groups.Count) > 4 && (match.Groups[4].Value == "false"))
                {
                    this.Predicate = (v) => !values.Contains(v);
                }
                else
                {
                    this.Predicate = (v) => values.Contains(v);
                }
            }

            if (this.Predicate != null)
            {
                this.FieldName = match.Groups[1].Value;
            }
        }