示例#1
0
        public IQueryable Apply(IQueryable data)
        {
            if (!string.IsNullOrWhiteSpace(Select)
                || !string.IsNullOrWhiteSpace(Label)
                || !string.IsNullOrWhiteSpace(Options)
                || !string.IsNullOrWhiteSpace(Format)
                || !string.IsNullOrWhiteSpace(Pivot)
                || !string.IsNullOrWhiteSpace(GroupBy))
            {
                throw new NotSupportedException("One or more clauses in this query are not supported.");
            }

            if (!string.IsNullOrWhiteSpace(Where))
            {
                if (Where.Contains(" and ") || Where.Contains(" or ") || Where.Contains(" not ")) throw new NotSupportedException("Multiple criteria in the where clause are not supported.");

                var match = _whereRegex.Match(Where);

                if (!match.Success)
                {
                    throw new FormatException("The 'Where' clause is in an invalid format.");
                }

                var left = match.Groups["operand1"].Value;
                var right = match.Groups["operand2"].Value;
                var op = match.Groups["operator"].Value;

                //HACK: assuming left is property and right is constant
                var propertyName = left;
                var constantValue = right.Trim().Trim('\'', '"');

                switch (op)
                {
                    case "contains":
                        data = data.WherePropertyContains(propertyName, constantValue, StringComparison.OrdinalIgnoreCase);
                        break;
                    case "like":
                    case "starts with":
                    case "ends with":
                    case "<=":
                    case "<":
                    case ">":
                    case ">=":
                    case "=":
                    case "!=":
                    case "<>":
                        throw new NotSupportedException(string.Format("The operator '{0}' is not yet supported.", op));
                }
            }

            if (!string.IsNullOrWhiteSpace(OrderBy))
            {
                if (OrderBy.Contains(",")) throw new NotSupportedException("Multiple column ordering not supported.");

                var desc = OrderBy.EndsWith("desc");
                var columnName = OrderBy.Replace(" asc", string.Empty).Replace(" desc", string.Empty).Trim(' ', '`');

                if (desc)
                {
                    data = data.OrderByDescending(columnName);
                }
                else
                {
                    data = data.OrderBy(columnName);
                }
            }

            //Offset and Limit have to be in this order...
            if (Offset != null)
            {
                data = data.Skip(Offset.Value);
            }

            if (Limit != null)
            {
                data = data.Take(Limit.Value);
            }

            return data;
        }