Ejemplo n.º 1
0
        /// <summary>
        /// Applies the $select and $expand query options to the given entity using the given <see cref="ODataQuerySettings"/>.
        /// </summary>
        /// <param name="entity">The original entity.</param>
        /// <param name="settings">The <see cref="ODataQuerySettings"/> that contains all the query application related settings.</param>
        /// <returns>The new entity after the $select and $expand query has been applied to.</returns>
        public object ApplyTo(object entity, ODataQuerySettings settings)
        {
            if (entity == null)
            {
                throw Error.ArgumentNull(nameof(entity));
            }
            if (settings == null)
            {
                throw Error.ArgumentNull(nameof(settings));
            }
            if (Context.ElementClrType == null)
            {
                throw Error.NotSupported(SRResources.ApplyToOnUntypedQueryOption, "ApplyTo");
            }

            ISelectExpandBinder binder = Context.GetSelectExpandBinder();

            QueryBinderContext binderContext = new QueryBinderContext(Context.Model, settings, Context.ElementClrType)
            {
                NavigationSource = Context.NavigationSource,
            };

            if (Compute != null)
            {
                binderContext.AddComputedProperties(Compute.ComputeClause.ComputedItems);
            }

            return(binder.ApplyBind(entity, _selectExpandClause, binderContext));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Translate an OData $select or $expand parse tree represented by <see cref="SelectExpandClause"/> to
        /// an <see cref="Expression"/> and applies it to an <see cref="object"/>.
        /// </summary>
        /// <param name="binder">The built in <see cref="ISelectExpandBinder"/></param>
        /// <param name="source">The original <see cref="object"/>.</param>
        /// <param name="selectExpandClause">The OData $select or $expand parse tree.</param>
        /// <param name="context">An instance of the <see cref="QueryBinderContext"/>.</param>
        /// <returns></returns>
        public static object ApplyBind(this ISelectExpandBinder binder, object source, SelectExpandClause selectExpandClause, QueryBinderContext context)
        {
            if (binder == null)
            {
                throw Error.ArgumentNull(nameof(binder));
            }

            if (source == null)
            {
                throw Error.ArgumentNull(nameof(source));
            }

            if (selectExpandClause == null)
            {
                throw Error.ArgumentNull(nameof(selectExpandClause));
            }

            if (context == null)
            {
                throw Error.ArgumentNull(nameof(context));
            }

            LambdaExpression projectionLambda = binder.BindSelectExpand(selectExpandClause, context) as LambdaExpression;

            return(projectionLambda.Compile().DynamicInvoke(source));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Translate an OData $select or $expand parse tree represented by <see cref="SelectExpandClause"/> to
        /// an <see cref="Expression"/> and applies it to an <see cref="IQueryable"/>. ALso <see cref="IQueryable"/>
        /// </summary>
        /// <param name="binder">The built in <see cref="ISelectExpandBinder"/></param>
        /// <param name="source">The original <see cref="IQueryable"/>.</param>
        /// <param name="selectExpandClause">The OData $select or $expand parse tree.</param>
        /// <param name="context">An instance of the <see cref="QueryBinderContext"/>.</param>
        /// <returns></returns>
        public static IQueryable ApplyBind(this ISelectExpandBinder binder, IQueryable source, SelectExpandClause selectExpandClause, QueryBinderContext context)
        {
            if (binder == null)
            {
                throw Error.ArgumentNull(nameof(binder));
            }

            if (source == null)
            {
                throw Error.ArgumentNull(nameof(source));
            }

            if (selectExpandClause == null)
            {
                throw Error.ArgumentNull(nameof(selectExpandClause));
            }

            if (context == null)
            {
                throw Error.ArgumentNull(nameof(context));
            }

            Type elementType = context.ElementClrType;

            LambdaExpression projectionLambda = binder.BindSelectExpand(selectExpandClause, context) as LambdaExpression;

            MethodInfo selectMethod = ExpressionHelperMethods.QueryableSelectGeneric.MakeGenericMethod(elementType, projectionLambda.Body.Type);

            return(selectMethod.Invoke(null, new object[] { source, projectionLambda }) as IQueryable);
        }
        /// <summary>
        /// Gets the <see cref="ISelectExpandBinder"/>.
        /// </summary>
        /// <param name="context">The query context.</param>
        /// <returns>The built <see cref="ISelectExpandBinder"/>.</returns>
        public static ISelectExpandBinder GetSelectExpandBinder(this ODataQueryContext context)
        {
            if (context == null)
            {
                throw Error.ArgumentNull(nameof(context));
            }

            ISelectExpandBinder binder = context.RequestContainer?.GetService <ISelectExpandBinder>();

            return(binder ?? new SelectExpandBinder(context.GetFilterBinder(), context.GetOrderByBinder()));
        }