Skip to content
This repository was archived by the owner on Dec 29, 2020. It is now read-only.

Parsing Query Options

Trevor Pilley edited this page Oct 5, 2020 · 8 revisions

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

Select

The ODataQueryOptions.Select property represents the $select query option and contains the following:

PropertyPaths

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

Raw Value is the URL decoded original value from the query string.

queryOptions.Select.RawValue;                          // "$select=FirstName,LastName,DateOfBirth"

Filter

The ODataQueryOptions.Filter property represents the $filter query option and contains the following:

Expression

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;

Binary Operator Node

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 node

We 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.Constant

Constant Node

A 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)

Property Access Node

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

Raw Value is the URL decoded original value from the query string.

queryOptions.Filter.RawValue;                          // "$filter=DateOfBirth gt 1990-01-01"

OrderBy

The ODataQueryOptions.OrderBy property represents the $orderby query option and contains the following:

Properties

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

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.

Clone this wiki locally