public HttpResponseMessage TryExecute <TResult>(Expression expression, out TResult result) { result = default(TResult); var elementType = TypeSystem.GetElementType(typeof(TResult)); var uri = BuildRequestUri(expression, elementType); // http://localhost:20000/products?$skip=1&$top=1 // Append optional search options. if (this.query.Options != null) { var builder = new UriBuilder(uri); HttpNameValueCollection querystring = HttpNameValueCollection.Parse(builder.Query.TrimStart('?')); querystring.Add(this.query.Options); builder.Query = querystring.ToString(); uri = builder.Uri; } #if DEBUG Debug.WriteLine("Query uri: " + uri.AbsoluteUri); #endif var response = this.query.EntityClient.http.Get(uri); if (response.IsSuccessStatusCode) { result = this.query.EntityClient.EntityFormatter.FromContent <TResult>(response.Content); } return(response); }
/// <summary> /// Creates a query for the given entity type, that will be /// executed when the queryable is enumerated. /// </summary> /// <typeparam name="T">Type of entity being queried.</typeparam> /// <param name="resourcePath">The resource path.</param> /// <param name="options">Additional query options expressed as an anonymous type /// where the property names and their values are used to populate a <see cref="NameValueCollection"/> /// automatically and are sent as query string parameters. Useful to overcome limitations /// in the underlying query support in WCF.</param> /// <returns>The query object which can be subsequently filtered with Where, ordered, take/skip, etc., which is /// run on the server side when it's enumerated.</returns> public IHttpEntityQuery <T> Query <T>(string resourcePath, object options) { var collection = new HttpNameValueCollection(); foreach (var prop in options.GetType() .GetProperties() .Where(x => !x.IsSpecialName && x.CanRead && x.GetGetMethod().GetParameters().Length == 0) .Select(x => new { Name = x.Name, Value = x.GetValue(options, null) }) .Where(x => x.Value != null)) { collection.Add(prop.Name, prop.Value.ToString()); } return(Query <T>(resourcePath, collection)); }
public static HttpNameValueCollection Parse(string query) { var tokens = query .Split(new char [] { '&' }, StringSplitOptions.RemoveEmptyEntries) .Select(Uri.UnescapeDataString); HttpNameValueCollection result = new HttpNameValueCollection(); tokens .Select(t => t.Split(new char [] { '=' }, StringSplitOptions.RemoveEmptyEntries)) .Select(pair => new { Key = pair[0], Value = (pair.Length > 1) ? pair[1] : "" }) .ToList() .ForEach(pair => result.Add(pair.Key, pair.Value)); return(result); }