public void Count_Returns_CorrectCountFunc()
        {
            // Arrange & Act
            Func <long> countFunc = ExpressionHelpers.Count(_querable, typeof(ECustomer));

            // Assert
            Assert.Equal(3, countFunc());
        }
 /// <summary>
 /// Gets the number of entities that satify the given query if the response should include an inline count, or <c>null</c> otherwise.
 /// </summary>
 /// <param name="query">The query to compute the count for.</param>
 /// <returns>The number of entities that satisfy the specified query if the response should include an inline count, or <c>null</c> otherwise.</returns>
 public long?GetEntityCount(IQueryable query)
 {
     if (Value == InlineCountValue.AllPages)
     {
         return(ExpressionHelpers.Count(query, Context.ElementClrType));
     }
     else
     {
         return(null);
     }
 }
Exemple #3
0
        /// <summary>
        /// Gets the Func of entities number that satisfy the given query if the response should include a count query option, or <c>null</c> otherwise.
        /// </summary>
        /// <param name="query">The query to compute the count for.</param>
        /// <returns>The the Func of entities number that satisfy the specified query if the response should include a count query option, or <c>null</c> otherwise.</returns>
        internal Func <long> GetEntityCountFunc(IQueryable query)
        {
            if (Context.ElementClrType == null)
            {
                throw Error.NotSupported(SRResources.ApplyToOnUntypedQueryOption, "GetEntityCount");
            }

            if (Value)
            {
                return(ExpressionHelpers.Count(query, Context.ElementClrType));
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// Gets the number of entities that satify the given query if the response should include an inline count, or <c>null</c> otherwise.
        /// </summary>
        /// <param name="query">The query to compute the count for.</param>
        /// <returns>The number of entities that satisfy the specified query if the response should include an inline count, or <c>null</c> otherwise.</returns>
        public long?GetEntityCount(IQueryable query)
        {
            if (Context.ElementClrType == null)
            {
                throw Error.NotSupported(SRResources.ApplyToOnUntypedQueryOption, "GetEntityCount");
            }

            if (Value == InlineCountValue.AllPages)
            {
                return(ExpressionHelpers.Count(query, Context.ElementClrType));
            }
            else
            {
                return(null);
            }
        }
Exemple #5
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TElement"></typeparam>
        /// <param name="context"></param>
        /// <param name="query"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <QueryResult> ExecuteQueryAsync <TElement>(QueryContext context, IQueryable <TElement> query, CancellationToken cancellationToken)
        {
            var countOption = context.GetApiService <RestierQueryExecutorOptions>();

            if (countOption.IncludeTotalCount)
            {
                var countQuery = ExpressionHelpers.GetCountableQuery(query);
                var expression = ExpressionHelpers.Count(countQuery.Expression, countQuery.ElementType);
                var result     = await ExecuteExpressionAsync <long>(context, countQuery.Provider, expression, cancellationToken).ConfigureAwait(false);

                var totalCount = result.Results.Cast <long>().Single();

                countOption.SetTotalCount(totalCount);
            }

            return(await Inner.ExecuteQueryAsync(context, query, cancellationToken).ConfigureAwait(false));
        }
Exemple #6
0
        /// <summary>
        /// Implement the Count-Distinct aggregation method
        /// </summary>
        /// <param name="elementType">The type of entities</param>
        /// <param name="query">The collection</param>
        /// <param name="transformation">The transformation clause created by the parser</param>
        /// <param name="propertyToAggregateExpression">Projection Expression that defines access to the property to aggregate</param>
        /// <param name="parameters">A list of string parameters sent to the aggregation method</param>
        /// <returns>The Sum result</returns>
        public override object DoAggregatinon(Type elementType, IQueryable collection, ApplyAggregateClause transformation, LambdaExpression propertyToAggregateExpression, params string[] parameters)
        {
            var propertyType   = GetAggregatedPropertyType(elementType, transformation.AggregatableProperty);
            var selectedValues = GetSelectedValues(elementType, collection, transformation, propertyToAggregateExpression);

            //call: (selected.AsQueryable() as IQueryable<double>).Ditinct();
            var distinct = ExpressionHelpers.Distinct(propertyType, selectedValues);

            try
            {
                //call: (distinct.AsQueryable() as IQueryable<double>).Count();
                return(ExpressionHelpers.Count(propertyType, distinct));
            }
            catch (TargetInvocationException)
            {
                //salve a problem in mongo that throw the error "No further operators may follow Distinct in a LINQ query." when trying to construct the expression tree.
                distinct = ExpressionHelpers.Cast(propertyType, distinct.AllElements().AsQueryable());
                return(ExpressionHelpers.Count(propertyType, distinct));
            }
        }
Exemple #7
0
        /// <summary>
        /// Asynchronously executes the query flow.
        /// </summary>
        /// <param name="context">
        /// The query context.
        /// </param>
        /// <param name="cancellationToken">
        /// A cancellation token.
        /// </param>
        /// <returns>
        /// A task that represents the asynchronous
        /// operation whose result is a query result.
        /// </returns>
        public static async Task <QueryResult> QueryAsync(
            QueryContext context,
            CancellationToken cancellationToken)
        {
            Ensure.NotNull(context, "context");

            // process query expression
            var expression = context.Request.Expression;
            var visitor    = new QueryExpressionVisitor(context);

            expression = visitor.Visit(expression);

            // get element type
            Type elementType = null;
            var  queryType   = expression.Type.FindGenericType(typeof(IQueryable <>));

            if (queryType != null)
            {
                elementType = queryType.GetGenericArguments()[0];
            }

            // append count expression if requested
            if (elementType != null && context.Request.ShouldReturnCount)
            {
                expression  = ExpressionHelpers.Count(expression, elementType);
                elementType = null; // now return type is single int
            }

            // execute query
            QueryResult result;
            var         executor = context.GetApiService <IQueryExecutor>();

            if (executor == null)
            {
                throw new NotSupportedException(Resources.QueryExecutorMissing);
            }

            if (elementType != null)
            {
                var query  = visitor.BaseQuery.Provider.CreateQuery(expression);
                var method = typeof(IQueryExecutor)
                             .GetMethod("ExecuteQueryAsync")
                             .MakeGenericMethod(elementType);
                var parameters = new object[]
                {
                    context, query, cancellationToken
                };
                var task = method.Invoke(executor, parameters) as Task <QueryResult>;
                result = await task;

                await CheckSubExpressionResult(
                    context, cancellationToken, result.Results, visitor, executor, expression);
            }
            else
            {
                var method = typeof(IQueryExecutor)
                             .GetMethod("ExecuteExpressionAsync")
                             .MakeGenericMethod(expression.Type);
                var parameters = new object[]
                {
                    context, visitor.BaseQuery.Provider, expression, cancellationToken
                };
                var task = method.Invoke(executor, parameters) as Task <QueryResult>;
                result = await task;
            }

            if (result != null)
            {
                result.ResultsSource = visitor.EntitySet;
            }

            return(result);
        }