/// <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.ToArray())); 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 <string, string>(Uri.EscapeDataString).ToArray <string>())); separator = '&'; } // Add the total count if (this.IncludeTotalCount) { text.AppendFormat(CultureInfo.InvariantCulture, "{0}{1}=allpages", separator, ODataOptions.InlineCount); separator = '&'; } return(text.ToString()); }
public void ToODataString_Null_ReturnsEmpty() { Assert.Equal("", ODataExpressionVisitor.ToODataString(null)); }