- 
                Notifications
    
You must be signed in to change notification settings  - Fork 202
 
Using isof and cast
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.
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
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
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
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
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