/// <summary>
        /// Apply $filter parameter to query.
        /// </summary>
        /// <param name="query">
        /// The OData aware query.
        /// </param>
        /// <param name="filterText">
        /// The $filter parameter text.
        /// </param>
        /// <param name="entitySetName">
        /// The entity set name.
        /// </param>
        /// <typeparam name="T">
        /// The query type param
        /// </typeparam>
        /// <returns>
        /// The <see cref="ODataQuery{T}"/> query with applied filter parameter.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// Argument Null Exception
        /// </exception>
        public static ODataQuery <T> Filter <T>(this ODataQuery <T> query, string filterText, string entitySetName = null)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }
            if (filterText == null)
            {
                throw new ArgumentNullException(nameof(filterText));
            }

            IEdmModel edmModel = query.EdmModel;

            ODataQueryOptionParser queryOptionParser = GetParser(
                query,
                entitySetName,
                new Dictionary <string, string> {
                { "$filter", filterText }
            });

            ODataSettings settings = query.ServiceProvider.GetRequiredService <ODataSettings>();

            FilterClause    filterClause     = queryOptionParser.ParseFilter();
            SingleValueNode filterExpression = filterClause.Expression.Accept(
                new ParameterAliasNodeTranslator(queryOptionParser.ParameterAliasNodes)) as SingleValueNode;

            filterExpression = filterExpression ?? new ConstantNode(null);
            filterClause     = new FilterClause(filterExpression, filterClause.RangeVariable);
            Contract.Assert(filterClause != null);

            var validator = new FilterQueryValidator(settings.DefaultQuerySettings);

            validator.Validate(filterClause, settings.ValidationSettings, edmModel);

            Expression filter = FilterBinder.Bind(query, filterClause, typeof(T), query.ServiceProvider);
            var        result = ExpressionHelpers.Where(query, filter, typeof(T));

            return(new ODataQuery <T>(result, query.ServiceProvider));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Validates the given OData query using the specified validation settings.
        /// </summary>
        /// <typeparam name="TEntity">The type of entity.</typeparam>
        /// <param name="options">The OData query to validate.</param>
        /// <param name="settings">The validation settings.</param>
        public virtual void Validate <TEntity>(ODataOptions <TEntity> options, ValidationSettings settings)
        {
            Requires.NotNull(options, nameof(options));
            Requires.NotNull(settings, nameof(settings));

            if (options.Count != null)
            {
                ValidateAllowed(AllowedOptions.Count, settings);
            }

            if (options.RawValues.DeltaToken != null)
            {
                ValidateAllowed(AllowedOptions.DeltaToken, settings);
            }

            if (options.RawValues.Format != null)
            {
                ValidateAllowed(AllowedOptions.Format, settings);
            }

            if (options.Filter != null)
            {
                ValidateAllowed(AllowedOptions.Filter, settings);
                filterValidator.Validate(options.Filter, settings);
            }

            if (options.OrderBy != null)
            {
                ValidateAllowed(AllowedOptions.OrderBy, settings);
            }

            if (options.Search != null)
            {
                ValidateAllowed(AllowedOptions.Search, settings);
            }

            if (options.RawValues.Select != null)
            {
                ValidateAllowed(AllowedOptions.Select, settings);
            }

            if (options.RawValues.Expand != null)
            {
                ValidateAllowed(AllowedOptions.Expand, settings);
            }

            if (options.Skip != null)
            {
                ValidateAllowed(AllowedOptions.Skip, settings);
                skipValidator.Validate(options.Skip, settings);
            }

            if (options.RawValues.SkipToken != null)
            {
                ValidateAllowed(AllowedOptions.SkipToken, settings);
            }

            if (options.Top != null)
            {
                ValidateAllowed(AllowedOptions.Top, settings);
                topValidator.Validate(options.Top, settings);
            }
        }