public static string ToODataString(QueryNode filter)
        {
            if (filter == null)
            {
                return(String.Empty);
            }
            var visitor = new ODataExpressionVisitor();

            filter.Accept(visitor);
            return(visitor.Expression.ToString());
        }
Example #2
0
        /// <summary>
        /// Convert the query structure into the standard OData URI protocol
        /// for queries.
        /// </summary>
        /// <returns>
        /// URI fragment representing the query.
        /// </returns>
        public string ToODataString()
        {
            char?         separator = null;
            StringBuilder text      = new StringBuilder();

            // Add the filter
            if (this.Filter != null)
            {
                string filterStr = ODataExpressionVisitor.ToODataString(this.Filter);
                text.AppendFormat(CultureInfo.InvariantCulture, "{0}{1}={2}", separator, ODataOptions.Filter, filterStr);
                separator = '&';
            }

            // Add the ordering
            if (this.Ordering.Count > 0)
            {
                IEnumerable <string> orderings = this.Ordering
                                                 .Select(o =>
                {
                    string result = ODataExpressionVisitor.ToODataString(o.Expression);
                    if (o.Direction == OrderByDirection.Descending)
                    {
                        result += " desc";
                    }
                    return(result);
                });

                text.AppendFormat(CultureInfo.InvariantCulture, "{0}{1}={2}", separator, ODataOptions.OrderBy, string.Join(",", orderings));
                separator = '&';
            }

            // Skip any elements
            if (this.Skip.HasValue && this.Skip >= 0)
            {
                text.AppendFormat(CultureInfo.InvariantCulture, "{0}{1}={2}", separator, ODataOptions.Skip, this.Skip);
                separator = '&';
            }

            // Take the desired number of elements
            if (this.Top.HasValue && this.Top >= 0)
            {
                text.AppendFormat(CultureInfo.InvariantCulture, "{0}{1}={2}", separator, ODataOptions.Top, this.Top);
                separator = '&';
            }

            // Add the selection
            if (this.Selection.Count > 0)
            {
                text.AppendFormat(CultureInfo.InvariantCulture, "{0}{1}={2}", separator, ODataOptions.Select, string.Join(",", this.Selection.Select(Uri.EscapeDataString)));
                separator = '&';
            }

            // Add the total count
            if (this.IncludeTotalCount)
            {
                text.AppendFormat(CultureInfo.InvariantCulture, "{0}{1}=allpages", separator, ODataOptions.InlineCount);
                separator = '&';
            }

            return(text.ToString());
        }