public void JsonSerializationWithSimpleContent()
        {
            var inlineCount = new InlineCount<int>(new[] { 1, 2, 3 }, count: 5);

            var result = JsonConvert.SerializeObject(inlineCount);

            Assert.Equal("{\"__count\":5,\"results\":[1,2,3]}", result);
        }
        public void JsonSerializationWithDynamicContent()
        {
            dynamic item = new ExpandoObject();
            item.Id = 14225;
            item.Name = "Fred";

            var inlineCount = new InlineCount<dynamic>(new[] { item }, count: 12);

            var result = JsonConvert.SerializeObject(inlineCount);

            Assert.Equal("{\"__count\":12,\"results\":[{\"Id\":14225,\"Name\":\"Fred\"}]}", result);
        }
        public void JsonSerializationWithClassContent()
        {
            var item = new Thing();
            item.Name = "Coffee";
            item.Total = 2.55M;

            var inlineCount = new InlineCount<Thing>(new[] { item }, count: 12);

            var result = JsonConvert.SerializeObject(inlineCount);

            Assert.Equal("{\"__count\":12,\"results\":[{\"Name\":\"Coffee\",\"Total\":2.55}]}", result);
        }
        public void XmlSerializationWithSimpleContent()
        {
            var inlineCount = new InlineCount<int>(new[] { 1, 2, 3 }, count: 5);

            var stringBuilder = new StringBuilder();
            using (var xmlWriter = XmlWriter.Create(stringBuilder, new XmlWriterSettings { Indent = false, OmitXmlDeclaration = true }))
            {
                var serializer = new DataContractSerializer(inlineCount.GetType());
                serializer.WriteObject(xmlWriter, inlineCount);
            }

            var result = stringBuilder.ToString();

            Assert.Equal("<InlineCountOfint xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/Net.Http.WebApi.OData\"><__count>5</__count><results xmlns:d2p1=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\"><d2p1:int>1</d2p1:int><d2p1:int>2</d2p1:int><d2p1:int>3</d2p1:int></results></InlineCountOfint>", result);
        }
        public void XmlSerializationWithClassContent()
        {
            var item = new Thing();
            item.Name = "Coffee";
            item.Total = 2.55M;

            var inlineCount = new InlineCount<Thing>(new[] { item }, count: 12);

            var stringBuilder = new StringBuilder();
            using (var xmlWriter = XmlWriter.Create(stringBuilder, new XmlWriterSettings { Indent = false, OmitXmlDeclaration = true }))
            {
                var serializer = new DataContractSerializer(inlineCount.GetType());
                serializer.WriteObject(xmlWriter, inlineCount);
            }

            var result = stringBuilder.ToString();

            Assert.Equal("<InlineCountOfInlineCountSerializationTests.ThingAeZ_PiJ7K xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.datacontract.org/2004/07/Net.Http.WebApi.OData\"><__count>12</__count><results xmlns:d2p1=\"http://schemas.datacontract.org/2004/07/Net.Http.WebApi.Tests.OData\"><d2p1:InlineCountSerializationTests.Thing><d2p1:Name>Coffee</d2p1:Name><d2p1:Total>2.55</d2p1:Total></d2p1:InlineCountSerializationTests.Thing></results></InlineCountOfInlineCountSerializationTests.ThingAeZ_PiJ7K>", result);
        }
Example #6
0
        /// <summary>
        /// Apply the individual query to the given IQueryable in the right order.
        /// </summary>
        /// <param name="query">The original <see cref="IQueryable"/>.</param>
        /// <param name="querySettings">The settings to use in query composition.</param>
        /// <returns>The new <see cref="IQueryable"/> after the query has been applied to.</returns>
        public virtual IQueryable ApplyTo(IQueryable query, ODataQuerySettings querySettings)
        {
            if (query == null)
            {
                throw Error.ArgumentNull("query");
            }

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

            IQueryable result = query;

            // Construct the actual query and apply them in the following order: filter, orderby, skip, top
            if (Filter != null)
            {
                result = Filter.ApplyTo(result, querySettings, _assembliesResolver);
            }

            if (InlineCount != null)
            {
                long?count = InlineCount.GetEntityCount(result);
                if (count.HasValue)
                {
                    Request.SetInlineCount(count.Value);
                }
            }

            OrderByQueryOption orderBy = OrderBy;

            // $skip or $top require a stable sort for predictable results.
            // Result limits require a stable sort to be able to generate a next page link.
            // If either is present in the query and we have permission,
            // generate an $orderby that will produce a stable sort.
            if (querySettings.EnsureStableOrdering &&
                (Skip != null || Top != null || querySettings.PageSize.HasValue))
            {
                // If there is no OrderBy present, we manufacture a default.
                // If an OrderBy is already present, we add any missing
                // properties necessary to make a stable sort.
                // Instead of failing early here if we cannot generate the OrderBy,
                // let the IQueryable backend fail (if it has to).
                orderBy = orderBy == null
                            ? GenerateDefaultOrderBy(Context)
                            : EnsureStableSortOrderBy(orderBy, Context);
            }

            if (orderBy != null)
            {
                result = orderBy.ApplyTo(result);
            }

            if (Skip != null)
            {
                result = Skip.ApplyTo(result, querySettings);
            }

            if (Top != null)
            {
                result = Top.ApplyTo(result, querySettings);
            }

            if (querySettings.PageSize.HasValue)
            {
                bool resultsLimited;
                result = LimitResults(result, querySettings.PageSize.Value, Context, out resultsLimited);
                if (resultsLimited && Request.RequestUri != null && Request.RequestUri.IsAbsoluteUri)
                {
                    Uri nextPageLink = GetNextPageLink(Request, querySettings.PageSize.Value);
                    Request.SetNextPageLink(nextPageLink);
                }
            }

            return(result);
        }
 public ODataInlineCountExpression(InlineCount count)
 {
     if (Enum.IsDefined(typeof(InlineCount), count))
     {
         throw new ArgumentException(string.Format("Undefined enum value on enum {0}.", count));
     }
     this.Value = count;
     this.ExpressionType = ExpressionType.Constant;
 }
 public WhenCreated()
 {
     this.inlineCount = new InlineCount<int>(results, count);
 }