Exemple #1
0
        public void CanApplyTop()
        {
            var model     = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetServiceModel();
            var topOption = new TopQueryOption("1", new ODataQueryContext(model, typeof(Customer), "Customers"));

            var customers = (new List <Customer> {
                new Customer {
                    CustomerId = 1, Name = "Andy"
                },
                new Customer {
                    CustomerId = 2, Name = "Aaron"
                },
                new Customer {
                    CustomerId = 3, Name = "Alex"
                }
            }).AsQueryable();

            var results = topOption.ApplyTo(customers).ToArray();

            Assert.Equal(1, results.Length);
            Assert.Equal(1, results[0].CustomerId);
        }
Exemple #2
0
        public void CanApplySkipTopOrderby()
        {
            var model         = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetServiceModel();
            var context       = new ODataQueryContext(model, typeof(Customer), "Customers");
            var orderbyOption = new OrderByQueryOption("Name", context);
            var skipOption    = new SkipQueryOption("2", context);
            var topOption     = new TopQueryOption("2", context);

            var customers = (new List <Customer> {
                new Customer {
                    CustomerId = 1, Name = "Andy"
                },
                new Customer {
                    CustomerId = 2, Name = "Aaron"
                },
                new Customer {
                    CustomerId = 3, Name = "Alex"
                },
                new Customer {
                    CustomerId = 4, Name = "Ace"
                },
                new Customer {
                    CustomerId = 5, Name = "Abner"
                }
            }).AsQueryable();

            IQueryable queryable = orderbyOption.ApplyTo(customers);

            queryable = skipOption.ApplyTo(queryable);
            queryable = topOption.ApplyTo(queryable);
            var results = ((IQueryable <Customer>)queryable).ToArray();

            Assert.Equal(2, results.Length);
            Assert.Equal(4, results[0].CustomerId);
            Assert.Equal(3, results[1].CustomerId);
        }
Exemple #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ODataQueryOptions"/> class based on the incoming request and some metadata information from
        /// the <see cref="ODataQueryContext"/>.
        /// </summary>
        /// <param name="context">The <see cref="ODataQueryContext"/> which contains the <see cref="IEdmModel"/> and some type information.</param>
        /// <param name="request">The incoming request message.</param>
        public ODataQueryOptions(ODataQueryContext context, HttpRequestMessage request)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }

            if (request.GetConfiguration() != null)
            {
                _assembliesResolver = request.GetConfiguration().Services.GetAssembliesResolver();
            }

            // fallback to the default assemblies resolver if none available.
            _assembliesResolver = _assembliesResolver ?? new DefaultAssembliesResolver();

            // remember the context and request
            Context = context;
            Request = request;

            // Parse the query from request Uri
            RawValues = new ODataRawQueryOptions();
            IEnumerable <KeyValuePair <string, string> > queryParameters = request.GetQueryNameValuePairs();

            foreach (KeyValuePair <string, string> kvp in queryParameters)
            {
                switch (kvp.Key)
                {
                case "$filter":
                    RawValues.Filter = kvp.Value;
                    ThrowIfEmpty(kvp.Value, "$filter");
                    Filter = new FilterQueryOption(kvp.Value, context);
                    break;

                case "$orderby":
                    RawValues.OrderBy = kvp.Value;
                    ThrowIfEmpty(kvp.Value, "$orderby");
                    OrderBy = new OrderByQueryOption(kvp.Value, context);
                    break;

                case "$top":
                    RawValues.Top = kvp.Value;
                    ThrowIfEmpty(kvp.Value, "$top");
                    Top = new TopQueryOption(kvp.Value, context);
                    break;

                case "$skip":
                    RawValues.Skip = kvp.Value;
                    ThrowIfEmpty(kvp.Value, "$skip");
                    Skip = new SkipQueryOption(kvp.Value, context);
                    break;

                case "$select":
                    RawValues.Select = kvp.Value;
                    break;

                case "$inlinecount":
                    RawValues.InlineCount = kvp.Value;
                    ThrowIfEmpty(kvp.Value, "$inlinecount");
                    InlineCount = new InlineCountQueryOption(kvp.Value, context);
                    break;

                case "$expand":
                    RawValues.Expand = kvp.Value;
                    break;

                case "$format":
                    RawValues.Format = kvp.Value;
                    break;

                case "$skiptoken":
                    RawValues.SkipToken = kvp.Value;
                    break;

                default:
                    // we don't throw if we can't recognize the query
                    break;
                }
            }

            Validator = new ODataQueryValidator();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ODataQueryOptions"/> class based on the incoming request and some metadata information from
        /// the <see cref="ODataQueryContext"/>.
        /// </summary>
        /// <param name="context">The <see cref="ODataQueryContext"/> which contains the <see cref="IEdmModel"/> and some type information</param>
        /// <param name="request">The incoming request message</param>
        public ODataQueryOptions(ODataQueryContext context, HttpRequestMessage request)
        {
            if (context == null)
            {
                throw Error.ArgumentNull("context");
            }

            if (request == null)
            {
                throw Error.ArgumentNull("request");
            }

            // remember the context
            Context = context;

            // Parse the query from request Uri
            RawValues = new ODataRawQueryOptions();
            IEnumerable <KeyValuePair <string, string> > queryParameters = request.GetQueryNameValuePairs();

            foreach (KeyValuePair <string, string> kvp in queryParameters)
            {
                switch (kvp.Key)
                {
                case "$filter":
                    RawValues.Filter = kvp.Value;
                    ThrowIfEmpty(kvp.Value, "$filter");
                    Filter = new FilterQueryOption(kvp.Value, context);
                    break;

                case "$orderby":
                    RawValues.OrderBy = kvp.Value;
                    ThrowIfEmpty(kvp.Value, "$orderby");
                    OrderBy = new OrderByQueryOption(kvp.Value, context);
                    break;

                case "$top":
                    RawValues.Top = kvp.Value;
                    ThrowIfEmpty(kvp.Value, "$top");
                    Top = new TopQueryOption(kvp.Value, context);
                    break;

                case "$skip":
                    RawValues.Skip = kvp.Value;
                    ThrowIfEmpty(kvp.Value, "$skip");
                    Skip = new SkipQueryOption(kvp.Value, context);
                    break;

                case "$select":
                    RawValues.Select = kvp.Value;
                    break;

                case "$inlinecount":
                    RawValues.InlineCount = kvp.Value;
                    break;

                case "$expand":
                    RawValues.Expand = kvp.Value;
                    break;

                case "$skiptoken":
                    RawValues.SkipToken = kvp.Value;
                    break;

                default:
                    // we don't throw if we can't recognize the query
                    break;
                }
            }
        }