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

Net.Http.OData.Query

Trevor Pilley edited this page Mar 24, 2020 · 14 revisions

The Query Options describe an OData query which has been made against the OData service, the following classes and their public members are contained within the namespace Net.Http.OData.Query.

Query

FilterQueryOption

Contains deserialised values from the $filter query option.

Property Description
QueryNode Expression { get; } Gets the expression.

FormatQueryOption

Contains deserialised values from the $format query option.

Property Description
MediaTypeHeaderValue MediaTypeHeaderValue { get; } Gets the media type header value.

IODataQueryOptionsValidator

The interface for an ODataQueryOptions validator.

Method Description
void Validate(ODataQueryOptions queryOptions, ODataValidationSettings validationSettings) Validates the specified query options using the specified validation settings.

ODataQueryOptions

Contains the query options in an OData query.

Property Description
bool Count { get; } Gets a value indicating whether the count query option has been specified.
EntitySet EntitySet { get; } Gets the EntitySet to apply the OData query against.
SelectExpandQueryOption Expand { get; } Gets SelectExpandQueryOption which represents the $expand query option.
FilterQueryOption Filter { get; } Gets FilterQueryOption which represents the $filter query option.
FormatQueryOption Format { get; } Gets FormatQueryOption which represents the $format query option.
OrderByQueryOption OrderBy { get; } Gets OrderByQueryOption which represents the $orderby query option.
ODataRawQueryOptions RawValues { get; } Gets the raw values of the OData query request.
SearchQueryOption Search { get; } Gets SearchQueryOption which represents the $search query option.
SelectExpandQueryOption Select { get; } Gets SelectExpandQueryOption which represents the $select query option.
int? Skip { get; } Gets the integer value specified in the $skip query option.
SkipTokenQueryOption SkipToken { get; } Gets SkipTokenQueryOption which represents the $skiptoken query option.
int? Top { get; } Gets the integer value specified in the $top query option.

ODataQueryOptionsValidator

Method Description
static IODataQueryOptionsValidator GetValidator(ODataVersion odataVersion) Gets the IODataQueryOptionsValidator for the specified ODataVersion.

ODataRawQueryOptions

ODataValidationSettings

OrderByProperty

Represents an order by property expression in the $orderby query option.

Property Description
OrderByDirection Direction { get; } Gets the direction the property should be ordered by.
PropertyPath PropertyPath { get; } Gets the property path to order by.
string RawValue { get; } Gets the raw request value.

OrderByQueryOption

Contains deserialised values from the $orderby query option.

Property Description
IReadOnlyList<OrderByProperty> Properties { get; } Gets the properties the query should be ordered by.

QueryOption

The base class for an OData System Query Option.

Property Description
string RawValue { get; } Gets the raw request value.

SearchQueryOption

Contains deserialised values from the $search query option.

SelectExpandQueryOption

Contains deserialised values from the $select or $expand query option.

Property Description
IReadOnlyList<PropertyPath> PropertyPaths { get; } Gets the property paths specified in the query.

SkipTokenQueryOption

Contains deserialised values from the $skiptoken query option.

Binders

The library contains 3 base classes to help with parsing OData Queries. Although you do not have to use them, they are intended to make it easier to implement parsing logic. The classes are in the namespace Net.Http.OData.Query.Binders.

AbstractFilterBinder

Provides a template for binding the FilterQueryOption

public class FilterBinder : AbstractFilterBinder
{
    private StringBuilder _stringBuilder;

    public FilterBinder(StringBuilder stringBuilder) => _stringBuilder = stringBuilder;

    protected override void Bind(BinaryOperatorNode binaryOperatorNode)
    {
        _stringBuilder.AppendLine();

        Bind(binaryOperatorNode.Left);

        _stringBuilder.Append(binaryOperatorNode.OperatorKind.ToString());

        Bind(binaryOperatorNode.Right);
    }

    protected override void Bind(ConstantNode constantNode)
    {
        if (constantNode.EdmType == null)
        {
            _stringBuilder.Append("NULL");
        }
        else
        {
            _stringBuilder.Append(constantNode.Value.ToString());
        }
    }

    protected override void Bind(FunctionCallNode functionCallNode)
    {
        _stringBuilder.Append(functionCallNode.Name);

        foreach (var parameter in functionCallNode.Parameters)
        {
            Bind(parameter);
        }
    }

    protected override void Bind(PropertyAccessNode propertyAccessNode)
    {
        var propertyPath = propertyAccessNode.PropertyPath;

        while (propertyPath != null)
        {
            _stringBuilder.Append(propertyPath.Property.Name);

            if (propertyPath.Next != null)
            {
                _stringBuilder.Append(".");
            }

            propertyPath = propertyPath.Next;
        }

        _stringBuilder.AppendLine();
    }

    protected override void Bind(UnaryOperatorNode unaryOperatorNode)
    {
        _stringBuilder.Append(unaryOperatorNode.OperatorKind.ToString());

        Bind(unaryOperatorNode.Operand);
    }
}

AbstractOrderByBinder

Provides a template for binding the OrderByQueryOption

public class OrderByBinder : AbstractOrderByBinder
{
    private StringBuilder _stringBuilder;

    public OrderByBinder(StringBuilder stringBuilder) => _stringBuilder = stringBuilder;

    protected override void Bind(OrderByProperty orderByProperty)
    {
        var propertyPath = orderByProperty.PropertyPath;

        while (propertyPath != null)
        {
            _stringBuilder.Append(propertyPath.Property.Name);

            if (propertyPath.Next != null)
            {
                _stringBuilder.Append(".");
            }

            propertyPath = propertyPath.Next;
        }

        _stringBuilder.Append(" ").AppendLine(orderByProperty.Direction.ToString());
    }
}

AbstractSelectExpandBinder

Provides a template for binding the SelectExpandQueryOption

public class SelectExpandBinder : AbstractSelectExpandBinder
{
    private StringBuilder _stringBuilder;

    public SelectExpandBinder(StringBuilder stringBuilder) => _stringBuilder = stringBuilder;

    protected override void Bind(PropertyPath propertyPath)
    {
        while (propertyPath != null)
        {
            _stringBuilder.Append(propertyPath.Property.Name);

            if (propertyPath.Next != null)
            {
                _stringBuilder.Append(".");
            }

            propertyPath = propertyPath.Next;
        }

        _stringBuilder.AppendLine();
    }
}

Example Usage

var stringBuilder = new StringBuilder();

var selectExpandBinder = new SelectExpandBinder(stringBuilder);
selectExpandBinder.Bind(queryOptions.Select);
selectExpandBinder.Bind(queryOptions.Expand);

var filterBinder = new FilterBinder(stringBuilder);
filterBinder.Bind(queryOptions.Filter);

var orderByBinder = new OrderByBinder(stringBuilder);
orderByBinder.Bind(queryOptions.OrderBy);

Expressions

BinaryOperatorNode

A QueryNode which represents a binary operator with a left and right branch.

Property Description
QueryNode Left { get; } Gets the left query node.
BinaryOperatorKind OperatorKind { get; } Gets the kind of the operator.
QueryNode Right { get; internal set; } Gets the right query node.

ConstantNode

A QueryNode which represents a constant value.

Property Description
EdmType EdmType { get; } Gets the EdmType of the value.
string LiteralText { get; } Gets the literal text if the constant value.
object Value { get; } Gets the constant value as an object.

FunctionCallNode

A QueryNode which represents a function call.

Property Description
string Name { get; } Gets the name of the function.
IReadOnlyList<QueryNode> Parameters { get; } Gets the parameters for the function call.

PropertyAccessNode

A QueryNode which represents a property.

Property Description
PropertyPath PropertyPath { get; } Gets the property path being referenced in the query.

PropertyPath

Represents a property path.

Property Description
PropertyPath Next { get; } Gets the next property in the path being referenced in the query, or null if this instance is the last (or only) property in the path.
EdmProperty Property { get; } Gets the EdmProperty representing the property being referenced in the query.

QueryNode

The base class for a query node.

Property Description
abstract QueryNodeKind Kind { get; } Gets the kind of query node.

UnaryOperatorNode

A QueryNode which represents a unary operator.

Property Description
QueryNode Operand { get; } Gets the operand of the unary operator.
UnaryOperatorKind OperatorKind { get; } Gets the kind of the operator.
Clone this wiki locally