forked from simplefx/Simple.OData
-
Notifications
You must be signed in to change notification settings - Fork 200
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.
var transport = await client
.For("Transport")
.Filter("isof('NorthwindModel.Ships')")
.FindEntryAsync();
Assert.Equal("Titanic", transport["ShipName"]);
var transport = await client
.For<Transport>()
.Filter(x => x is Ship)
.As<Ship>()
.FindEntryAsync();
Assert.Equal("Titanic", transport.ShipName);
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
var employee = await client
.For("Employees")
.Filter("isof(Superior, 'NorthwindModel.Employees')")
.FindEntryAsync();
Assert.NotNull(employee);
var employee = await client
.For<Employee>()
.Filter(x => x.Superior is Employee)
.FindEntryAsync();
Assert.NotNull(employee);
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
var product = await client
.For("Products")
.Filter("ProductID eq cast(1L, 'Edm.Int32')")
.FindEntryAsync();
Assert.NotNull(product);
var product = await client
.For<Product>()
.Filter(x => x.CategoryID == (int)1L)
.FindEntryAsync();
Assert.NotNull(product);
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
var employee = await client
.For("Employees")
.Filter("cast('NorthwindModel.Employees') ne null")
.FindEntryAsync();
Assert.NotNull(employee);
var employee = await client
.For<Employee>()
.Filter(x => x as Employee != null)
.FindEntryAsync();
Assert.NotNull(employee);
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
var employee = await client
.For("Employees")
.Filter("cast(Superior, 'NorthwindModel.Employees') ne null")
.FindEntryAsync();
Assert.NotNull(employee);
var employee = await client
.For<Employee>()
.Filter(x => x.Superior as Employee != null)
.FindEntryAsync();
Assert.NotNull(employee);
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