Esempio n. 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="queryable"></param>
        /// <param name="path"></param>
        /// <param name="applyCount"></param>
        /// <returns></returns>
        private async Task <(IQueryable Queryable, ETag Etag)> ApplyQueryOptionsAsync(IQueryable queryable, ODataPath path, bool applyCount)
        {
            ETag etag = null;

            if (shouldWriteRawValue)
            {
                // Query options don't apply to $value.
                return(queryable, null);
            }

            var properties = Request.ODataProperties();
            var model      = await api.GetModelAsync().ConfigureAwait(false);

            var queryContext = new ODataQueryContext(model, queryable.ElementType, path);
            var queryOptions = new ODataQueryOptions(queryContext, Request);

            // Get etag for query request
            if (queryOptions.IfMatch != null)
            {
                etag = queryOptions.IfMatch;
            }
            else if (queryOptions.IfNoneMatch != null)
            {
                etag = queryOptions.IfNoneMatch;
            }

            // TODO GitHubIssue#41 : Ensure stable ordering for query

            if (shouldReturnCount)
            {
                // Query options other than $filter and $search don't apply to $count.
                queryable = queryOptions.ApplyTo(queryable, querySettings, AllowedQueryOptions.All ^ AllowedQueryOptions.Filter);
                return(queryable, etag);
            }

            if (queryOptions.Count != null && !applyCount)
            {
                var queryExecutorOptions = api.GetApiService <RestierQueryExecutorOptions>();
                queryExecutorOptions.IncludeTotalCount = queryOptions.Count.Value;
                queryExecutorOptions.SetTotalCount     = value => properties.TotalCount = value;
            }

            // Validate query before apply, and query setting like MaxExpansionDepth can be customized here
            queryOptions.Validate(validationSettings);

            // Entity count can NOT be evaluated at this point of time because the source
            // expression is just a placeholder to be replaced by the expression sourcer.
            if (!applyCount)
            {
                queryable = queryOptions.ApplyTo(queryable, querySettings, AllowedQueryOptions.Count);
            }
            else
            {
                queryable = queryOptions.ApplyTo(queryable, querySettings);
            }

            return(queryable, etag);
        }