-
Notifications
You must be signed in to change notification settings - Fork 1
Parsing Query Options
Consider the following OData query:
http://server/odata/Customers?$select=FirstName,LastName,DateOfBirth&$filter=DateOfBirth gt 1990-01-01&$orderby=LastName desc,FirstName
In this instance, 3 of the OData Query Options are specified:
- $select
- $filter
- $orderby
The ODataQueryOptions.Select property represents the $select query option and contains the following:
PropertyPaths contains the property paths (expressed as PropertyPath objects) used in the Customer entity set.
queryOptions.Select.PropertyPaths.Count; // 3
queryOptions.Select.PropertyPaths[0].Property.Name; // "FirstName"
queryOptions.Select.PropertyPaths[1].Property.Name; // "LastName"
queryOptions.Select.PropertyPaths[2].Property.Name; // "DateOfBirth"Raw Value is the URL decoded original value from the query string.
queryOptions.Select.RawValue; // "$select=FirstName,LastName,DateOfBirth"The ODataQueryOptions.Filter property represents the $filter query option and contains the following:
The expression is an object graph which represents the query as QueryNode instances. There are a number of types of Query Node and the QueryNodeKind enumeration lists them. In our example above, we have a simple binary expression which would be represented as follows:
(BinaryOperatorKind)
------ gt ------
(Left) | | (Right)
DateOfBirth 1990-01-01
Firstly we need to know what kind of expression we are dealing with.
queryOptions.Filter.Expression.Kind; // QueryNodeKind.BinaryOperator
// Cast the expression as the indicated kind
var binaryOperatorNode = (BinaryOperatorNode)queryOptions.Filter.Expression;A Binary node has 3 properties, the left and right sides of the expression and an operator.
binaryOperatorNode.Left; // The left query node
binaryOperatorNode.OperatorKind; // BinaryOperatorKind.GreaterThan
binaryOperatorNode.Right; // The right query nodeWe can then check the QueryNodeKind of the left and right expressions to know what to cast them as:
binaryOperatorNode.Left.Kind; // QueryNodeKind.PropertyAccess
binaryOperatorNode.Right.Kind; // QueryNodeKind.ConstantA Constant node represents a value such as a date, string, integer etc and has 3 properties.
constantNode.EdmType.Name; // EdmPrimitiveType.Date
constantNode.LiteralText; // 1990-01-01
constantNode.Value; // new DateTime(1990, 01, 01)A property access node references the property paths (expressed as PropertyPath objects) used in the Customer entity set.
propertyAccessNode.PropertyPath.Property.Name; // "DateOfBirth"Raw Value is the URL decoded original value from the query string.
queryOptions.Filter.RawValue; // "$filter=DateOfBirth gt 1990-01-01"The ODataQueryOptions.OrderBy property represents the $orderby query option and contains the following:
Properties contains the property paths (expressed as PropertyPath objects) used in the Customer entity set contained within OrderByProperty objects.
queryOptions.OrderBy.Properties.Count; // 2
var orderByProperty0 = queryOptions.OrderBy.Properties[0];
orderByProperty0.Direction; // OrderByDirection.Descending
orderByProperty0.PropertyPath.Property; // The EdmProperty in the EntitySet
orderByProperty0.PropertyPath.Property.Name; // LastName
orderByProperty0.RawValue; // "LastName desc"
var orderByProperty1 = queryOptions.OrderBy.Properties[1];
orderByProperty1.Direction; // OrderByDirection.Ascending
orderByProperty1.PropertyPath.Property; // The EdmProperty in the EntitySet
orderByProperty1.PropertyPath.Property.Name; // FirstName
orderByProperty1.RawValue; // "FirstName"Raw Value is the URL decoded original value from the query string.
queryOptions.OrderBy.RawValue; // "$orderby=LastName desc,FirstName"See Binders for additional classes to help with binding an OData query expression to an underlying context.