Skip to content

Using isof and cast

Jason Finch edited this page Aug 23, 2018 · 6 revisions

OData protocol defines functions isof and cast that can be used to filter and cast resources and its properties to the specified type. Simple.OData.Client has support for these functions for all its syntax flavors.


Filter transport entities by those of a type Ship

Untyped syntax

var transport = await client
    .For("Transport")
    .Filter("isof('NorthwindModel.Ships')")
    .FindEntryAsync();
Assert.Equal("Titanic", transport["ShipName"]);

Typed syntax

var transport = await client
    .For<Transport>()
    .Filter(x => x is Ship)
    .As<Ship>()
    .FindEntryAsync();
Assert.Equal("Titanic", transport.ShipName);

Dynamic syntax

var x = ODataDynamic.Expression;
var transport = await client
    .For(x.Transport)
    .Filter(x.Is(typeof(Ship)))
    .FindEntryAsync();
Assert.Equal("Titanic", transport.ShipName);

Request URI: GET Transport?$filter=isof%28%27NorthwindModel.Ships%27%29


Filter employee's superiors by those of a type Employee

Untyped syntax

var employee = await client
    .For("Employees")
    .Filter("isof(Superior, 'NorthwindModel.Employees')")
    .FindEntryAsync();
Assert.NotNull(employee);

Typed syntax

var employee = await client
    .For<Employee>()
    .Filter(x => x.Superior is Employee)
    .FindEntryAsync();
Assert.NotNull(employee);

Dynamic syntax

var x = ODataDynamic.Expression;
var employee = await client
    .For(x.Employee)
    .Filter(x.Superior.Is(typeof(Employee)))
    .FindEntryAsync();
Assert.NotNull(employee);

Request URI: GET Employees?$filter=isof%28Superior%2C%20%27NorthwindModel.Employees%27%29


Cast a property to a primitive type

Untyped syntax

var product = await client
    .For("Products")
    .Filter("ProductID eq cast(1L, 'Edm.Int32')")
    .FindEntryAsync();
Assert.NotNull(product);

Typed syntax

var product = await client
    .For<Product>()
    .Filter(x => x.CategoryID == (int)1L)
    .FindEntryAsync();
Assert.NotNull(product);

Dynamic syntax

var x = ODataDynamic.Expression;
var product = await client
    .For(x.Product)
    .Filter(x.CategoryID == (int)1L)
    .FindEntryAsync();
Assert.NotNull(product);

Request URI: GET Products?$filter=ProductID%20eq%20cast%281L%2C%20%27Edm.Int32%27%29


Cast an instance to an entity type.

Untyped syntax

var employee = await client
    .For("Employees")
    .Filter("cast('NorthwindModel.Employees') ne null")
    .FindEntryAsync();
Assert.NotNull(employee);

Typed syntax

var employee = await client
    .For<Employee>()
    .Filter(x => x as Employee != null)
    .FindEntryAsync();
Assert.NotNull(employee);

Dynamic syntax

var x = ODataDynamic.Expression;
var employee = await _client
    .For(x.Employee)
    .Filter(x.As(typeof(Employee)) != null)
    .FindEntryAsync();
Assert.NotNull(employee);

Request URI: GET Employees?$filter=cast%28%27NorthwindModel.Employees%27%29%20ne%20null


Cast a property to an entity type.

Untyped syntax

var employee = await client
    .For("Employees")
    .Filter("cast(Superior, 'NorthwindModel.Employees') ne null")
    .FindEntryAsync();
Assert.NotNull(employee);

Typed syntax

var employee = await client
    .For<Employee>()
    .Filter(x => x.Superior as Employee != null)
    .FindEntryAsync();
Assert.NotNull(employee);

Dynamic syntax

var x = ODataDynamic.Expression;
var employee = await _client
    .For(x.Employee)
    .Filter(x.Superior.As(typeof(Employee)) != null)
    .FindEntryAsync();
Assert.NotNull(employee);

Request URI: GET Employees?$filter=cast%28Superior%2C%20%27NorthwindModel.Employees%27%29%20ne%20null


See also:
Retrieving data
OData URI conventions

Clone this wiki locally