Skip to content
Jason Finch edited this page Aug 23, 2018 · 24 revisions

OData supports updates only by resource key, so in case the entry is identified by non-key properties or multiple entries should be updated, corresponding entry keys should first be retrieved on a client side and only then Update request can be invoked for each key value. Simple.OData.Client simplifies non-key and multiple entry updates by supporting passing an OData filter expression to an update operation. Bear in mind however that filter-based updates will typicall result in execution of multiple OData requests.

OData adapter tries to optimize request payload and wherever possible sends PATCH command with only the changed data instead of PUT with the whole entry content.


Update a product by key

Untyped syntax

await client
    .For("Products")
    .Key(1)
    .Set(new { UnitPrice = 123m })
    .UpdateEntryAsync();

Typed syntax

await client
    .For<Products>()
    .Key(1)
    .Set(new { UnitPrice = 123m })
    .UpdateEntryAsync();

Dynamic syntax

var x = ODataFilter.Expression;
await client
    .For(x.Products)
    .Key(1)
    .Set(x.UnitPrice = 123m)
    .UpdateEntryAsync();

Request URI: GET Products?$filter=ProductName+eq+%27Chai%27
Request URI: PATCH Products(1)
Request content:

<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <title />
  <updated>2012-10-09T12:58:16.7080000Z</updated>
  <author>
    <name />
  </author>
  <id />
  <content type="application/xml">
    <m:properties>
      <d:UnitPrice m:type="Edm.Decimal">123</d:UnitPrice>
    </m:properties>
  </content>
</entry>

Update multiple products

Untyped syntax

await client
    .For("Products")
    .Filter("length(ProductName)+eq+4")
    .Set(new { UnitPrice = 123m })
    .UpdateEntriesAsync();

Typed syntax

await client
    .For<Products>()
    .Filter(x => x.ProductName.Length() == 4)
    .Set(new { UnitPrice = 123m })
    .UpdateEntriesAsync();

Dynamic syntax

var x = ODataFilter.Expression;
await client
    .For(x.Products)
    .Filter(x.ProductName.Length() == 4)
    .Set(x.UnitPrice = 123m)
    .UpdateEntriesAsync();

Request URI: N/A (separate PUT/PATCH request for each entry matching search criteria)


See also:
Updating entries with links
Modifying data