Ejemplo n.º 1
0
        /// <summary>
        /// Processes the $orderby portion of the uri. Will also add special ordering if the response would be paged.
        /// </summary>
        /// <param name="source">The source expression</param>
        /// <returns>The result of processing the orderby</returns>
        private QueryExpression ProcessOrderBy(QueryExpression source)
        {
            ExceptionUtilities.CheckArgumentNotNull(source, "source");

            var queryBasedUri = this.currentUri as QueryBasedODataUri;

            if (queryBasedUri != null && queryBasedUri.OrderByExpressions.Count > 0)
            {
                LinqOrderByExpression orderedQuery = null;

                // rebuild the orderby expression using OrderBy and ThenBy
                foreach (var orderby in queryBasedUri.OrderByExpressions)
                {
                    ExceptionUtilities.Assert(orderby.AreDescending.Count == orderby.KeySelectors.Count, "Orderby expression had mismatched counts");
                    ExceptionUtilities.Assert(orderby.KeySelectors.Count > 0, "Orderby expression did not have any key selectors");

                    // handle the first key selector seperately, since the remainder will need to use ThenBy
                    int startIndex = 0;
                    if (orderedQuery == null)
                    {
                        var ascending = !orderby.AreDescending[0];
                        var lambda    = orderby.KeySelectors[0];
                        startIndex = 1;

                        if (ascending)
                        {
                            orderedQuery = source.OrderBy(o => this.RewriteLambda(o, lambda));
                        }
                        else
                        {
                            orderedQuery = source.OrderByDescending(o => this.RewriteLambda(o, lambda));
                        }
                    }

                    // handle the remaining key selectors
                    for (int i = startIndex; i < orderby.AreDescending.Count; i++)
                    {
                        var ascending = !orderby.AreDescending[i];
                        var lambda    = orderby.KeySelectors[i];

                        if (ascending)
                        {
                            orderedQuery = orderedQuery.ThenBy(o => this.RewriteLambda(o, lambda));
                        }
                        else
                        {
                            orderedQuery = orderedQuery.ThenByDescending(o => this.RewriteLambda(o, lambda));
                        }
                    }
                }

                source = orderedQuery;
            }
            else
            {
                // if this uri was not built from a query, then we cannot handle orderby expressions
                var orderByString = this.currentUri.OrderBy;
                ExceptionUtilities.Assert(string.IsNullOrEmpty(orderByString), "Non-query-based uri had non-empty orderby string '{0}' which cannot be processed", orderByString);
            }

            // add ordering if the response would be paged
            if (this.IsPaged())
            {
                source = this.AddOrderingForPaging(source);
            }

            return(source);
        }