Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.
Trevor Pilley edited this page Feb 20, 2017 · 55 revisions

Welcome to the Net.Http.WebApi.OData wiki!

NuGet version

Net.Http.WebApi.OData is a C# library which parses an OData query uri into an object model which can be used to query custom data sources which are not IQueryable. It was extracted from the MicroLite.Extensions.WebApi library into a separate project so that it could be easily used by other projects.

Getting the OData Query Options

To use it in your own Web API, firstly install the nuget package Install-Package Net.Http.WebApi.OData and then in your controller, define a Get method which accepts a single parameter of ODataQueryOptions:

public IEnumerable<MyClass> Get(ODataQueryOptions queryOptions)
{
    // Implement query logic.
}

Validating the OData Query Options

You can use the ODataValidationSettings and the Validate extension method to validate the parameters:

public IEnumerable<MyClass> Get(ODataQueryOptions queryOptions)
{
    var validationSettings = new ODataValidationSettings
    {
        AllowedArithmeticOperators = AllowedArithmeticOperators.All,
        AllowedFunctions = AllowedFunctions.AllFunctions,
        AllowedLogicalOperators = AllowedLogicalOperators.All,
        AllowedQueryOptions = AllowedQueryOptions.Filter
            | AllowedQueryOptions.Format
            | AllowedQueryOptions.InlineCount
            | AllowedQueryOptions.OrderBy
            | AllowedQueryOptions.Select
            | AllowedQueryOptions.Skip
            | AllowedQueryOptions.Top,
        MaxTop = 50      
    };

    queryOptions.Validate(validationSettings);

    // Implement query logic.
}

Validate will throw an ODataException if the ODataQueryOptions contain any values which the service does not allow based upon the specified validation settings.

Using the OData Query Options

The ODataQueryOptions contains properties which can be interrogated to understand the specified query.

Consider the following OData query:

http://localhost/api/Customers?$select=FirstName,LastName,DateOfBirth&$orderby=LastName desc,FirstName

The ODataQueryOptions.Select would be as follows:

queryOptions.Select.RawValue;         // "$select=FirstName,LastName,DateOfBirth"
queryOptions.Select.Properties.Count; // 3
queryOptions.Select.Properties[0];    // "FirstName"
queryOptions.Select.Properties[1];    // "LastName"
queryOptions.Select.Properties[2];    // "DateOfBirth"

The ODataQueryOptions.OrderBy would be as follows:

queryOptions.OrderBy.RawValue;                // "$orderby=LastName desc,FirstName"
queryOptions.OrderBy.Properties.Count;        // 2

queryOptions.OrderBy.Properties[0].Direction; // OrderByDirection.Descending
queryOptions.OrderBy.Properties[0].Name;      // LastName
queryOptions.OrderBy.Properties[0].RawValue;  // "LastName desc"

queryOptions.OrderBy.Properties[1].Direction; // OrderByDirection.Ascending
queryOptions.OrderBy.Properties[1].Name;      // FirstName
queryOptions.OrderBy.Properties[1].RawValue;  // "FirstName"

Supported OData Query Options

Net.Http.WebApi.OData is currently built to support OData 3.0, the OData Query Options specify the following query options and Net.Http.WebApi.OData currently supports them if ticked:

$filter

Logical Operators
Operator Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
eq implemented implemented
ne implemented implemented
gt implemented implemented
ge implemented implemented
lt implemented implemented
le implemented implemented
and implemented implemented
or implemented implemented
not implemented implemented
Arithmetic Operators
Operator Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
add implemented implemented
sub implemented implemented
mul implemented implemented
div implemented implemented
mod implemented implemented

Grouping Operators

Operator Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
() Precedence Grouping implemented implemented
String Functions
Operator Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
substringof implemented not in spec (use contains)
endswith implemented implemented
startswith implemented implemented
length implemented implemented
indexof implemented implemented
replace implemented implemented
substring implemented implemented
tolower implemented implemented
toupper implemented implemented
trim implemented implemented
concat implemented implemented
contains not in spec (use substringof) implemented
Date Functions
Operator Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
day implemented implemented
hour implemented implemented
minute implemented implemented
month implemented implemented
second implemented implemented
year implemented implemented
now not in spec implemented
mindatetime not in spec implemented
maxdatetime not in spec implemented
fractionalseconds not in spec implemented
Math Functions
Operator Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
round implemented implemented
floor implemented implemented
ceiling implemented implemented
Type Functions
Operator Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
IsOf implemented implemented
Cast implemented implemented

$expand

Operator Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
$expand=Orders implemented implemented

$select

Operator Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
wildcard $select=* implemented implemented
properties $select=Name, DateOfBirth implemented implemented
nested properties $select=Address.Postcode not implemented not implemented

$orderby

Operator Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
asc $orderby=Name asc implemented implemented
desc $orderby=Name desc implemented implemented

$top

Operator Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
$top=5 implemented implemented

$skip

Operator Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
$skip=10 implemented implemented

$count

Operator Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
$count=true not in spec (use $inlinecount=allpages) implemented
$count=false not in spec (use $inlinecount=none) implemented

$inlinecount

Operator Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
$inlinecount=allpages implemented not in spec (replaced by $count=true)
$inlinecount=none implemented not in spec (replaced by $count=false)

$format

Operator Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
$format=atom implemented implemented
$format=json implemented implemented
$format=xml implemented implemented
$format=... where ... is application/? implemented implemented

Entity Key

URI Path Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
/Products(1) not implemented not implemented

$value

URI Path Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
/Products(1)/Name/$value not implemented not implemented

$links

URI Path Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
/Products(0)/$links/Orders not implemented not implemented

$count

URI Path Net.Http.WebApi.OData 3.4.x Net.Http.WebApi.OData 4.x
/Products/$count not implemented not implemented
Clone this wiki locally