/// <summary> /// Deserializes the query operations in the specified Uri and applies them /// to the specified IQueryable. /// </summary> /// <param name="query">The root query to compose the deserialized query over.</param> /// <param name="uri">The request Uri containing the query operations.</param> /// <returns>The resulting IQueryable with the deserialized query composed over it.</returns> public static IQueryable Deserialize(IQueryable query, Uri uri) { if (query == null) { throw Error.ArgumentNull("query"); } if (uri == null) { throw Error.ArgumentNull("uri"); } ServiceQuery serviceQuery = GetServiceQuery(uri); return(Deserialize(query, serviceQuery.QueryParts, null)); }
internal static ServiceQuery GetServiceQuery(Uri uri) { if (uri == null) { throw Error.ArgumentNull("uri"); } NameValueCollection queryPartCollection = UriQueryUtility.ParseQueryString(uri.Query); List <ServiceQueryPart> serviceQueryParts = new List <ServiceQueryPart>(); foreach (string queryPart in queryPartCollection) { if (queryPart == null || !queryPart.StartsWith("$", StringComparison.Ordinal)) { // not a special query string continue; } foreach (string value in queryPartCollection.GetValues(queryPart)) { string queryOperator = queryPart.Substring(1); if (!ServiceQuery.IsSupportedQueryOperator(queryOperator)) { // skip any operators we don't support continue; } ServiceQueryPart serviceQueryPart = new ServiceQueryPart(queryOperator, value); serviceQueryParts.Add(serviceQueryPart); } } // Query parts for OData need to be ordered $filter, $orderby, $skip, $top. For this // set of query operators, they are already in alphabetical order, so it suffices to // order by operator name. In the future if we support other operators, this may need // to be reexamined. serviceQueryParts = serviceQueryParts.OrderBy(p => p.QueryOperator).ToList(); ServiceQuery serviceQuery = new ServiceQuery() { QueryParts = serviceQueryParts, }; return(serviceQuery); }
/// <summary> /// Deserializes the query operations in the specified Uri and returns an IQueryable /// with a manufactured query root with those operations applied. /// </summary> /// <param name="elementType">The element type of the query</param> /// <param name="uri">The request Uri containing the query operations.</param> /// <returns>The resulting IQueryable with the deserialized query composed over it.</returns> public static IQueryable Deserialize(Type elementType, Uri uri) { if (elementType == null) { throw Error.ArgumentNull("elementType"); } if (uri == null) { throw Error.ArgumentNull("uri"); } ServiceQuery serviceQuery = GetServiceQuery(uri); Array array = Array.CreateInstance(elementType, 0); IQueryable baseQuery = ((IEnumerable)array).AsQueryable(); return(Deserialize(baseQuery, serviceQuery.QueryParts, null)); }
/// <summary> /// Public constructor /// </summary> /// <param name="queryOperator">The query operator</param> /// <param name="expression">The query expression</param> public ServiceQueryPart(string queryOperator, string expression) { if (queryOperator == null) { throw Error.ArgumentNull("queryOperator"); } if (expression == null) { throw Error.ArgumentNull("expression"); } if (!ServiceQuery.IsSupportedQueryOperator(queryOperator)) { throw Error.Argument("queryOperator", SRResources.InvalidQueryOperator, queryOperator); } QueryOperator = queryOperator; Expression = expression; }