Skip to content

Retrieving a single row by key

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

Every OData collection must define a key that consists of a single or multiple properties and can be used for entry lookup and addressing links between different OData collections.


Lookup a row by single numeric key

Untyped syntax

var product = await client
    .For("Products")
    .Key(1)
    .FindEntryAsync();
Assert.Equal("Chai", product["ProductName"]);

Typed syntax

var product = await client
    .For<Products>()
    .Key(1)
    .FindEntryAsync();
Assert.Equal("Chai", product.ProductName);

Dynamic syntax

var x = ODataDynamic.Expression;
var product = await client
    .For(x.Products)
    .Key(1)
    .FindEntryAsync();
Assert.Equal("Chai", product.ProductName);

Request URI: GET Products(1)


Lookup a row by single string key

Untyped syntax

var customer = await client
    .For("Customers")
    .Key("ALFKI")
    .FindEntryAsync();
Assert.Equal("ALFKI", customer["CustomerID"]);

Typed syntax

var customer = await client
    .For<Customers>()
    .Key("ALFKI")
    .FindEntryAsync();
Assert.Equal("ALFKI", customer.CustomerID);

Dynamic syntax

var x = ODataDynamic.Expression;
var customer = await client
    .For(x.Customers)
    .Key("ALFKI")
    .FindEntryAsync();
Assert.Equal("ALFKI", customer.CustomerID);

Request URI: GET Customers(%27ALFKI%27)


Lookup a row by compound key

Untyped syntax

var orderDetails = await client
    .For("OrderDetails")
    .Key(10248, 11)
    .FindEntryAsync();
Assert.Equal(10248, orderDetails["OrderID"]);

Typed syntax

var orderDetails = await client
    .For<OrderDetails>()
    .Key(10248, 11)
    .FindEntryAsync();
Assert.Equal(10248, orderDetails.OrderID);

Dynamic syntax

var x = ODataDynamic.Expression;
var orderDetails = await client
    .For(x.OrderDetails)
    .Key(10248, 11)
    .FindEntryAsync();
Assert.Equal(10248, orderDetails.OrderID);

Request URI: GET OrderDetails(OrderID%3d1%2cProductID%3d1)


See also:
Retrieving data