Skip to content

Linking and unlinking entries

Theodor Heiselberg edited this page Dec 11, 2018 · 21 revisions

Link a product to a category

Untyped syntax

var category = await client
    .For("Categories")
    .Set(new { CategoryName = "Test1" })
    .InsertEntryAsync();
var product = await client
    .For("Products")
    .Set(new { ProductName = "Test2" })
    .InsertEntryAsync();

await client
    .For("Products")
    .Key(product)
    .LinkEntryAsync("Category", category);

Typed syntax

var category = await client
    .For<Categories>()
    .Set(new { CategoryName = "Test1" })
    .InsertEntryAsync();
var product = await client
    .For<Products>()
    .Set(new { ProductName = "Test2" })
    .InsertEntryAsync();

await client
    .For<Products>()
    .Key(product)
    .LinkEntryAsync(x => x.Category, category);

Dynamic syntax

var x = ODataDynamic.Expression;
var category = await client
    .For(x.Categories)
    .Set(x.CategoryName = "Test1")
    .InsertEntryAsync();
var product = await client
    .For(x.Products)
    .Set(x.ProductName = "Test2")
    .InsertEntryAsync();

await client
    .For(x.Products)
    .Key(product)
    .LinkEntry(x.Category, category);

Request URI: PUT Products(79)/$links/Category
Request content:

<uri xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">Categories(10)</uri>

Link a product to a different category

Untyped syntax

var category = await client
    .For("Categories")
    .Set(new { CategoryName = "Test1" })
    .InsertEntryAsync();
var product = await client
    .For("Products")
    .Set(new { ProductName = "Test2", CategoryID = 1 })
    .InsertEntryAsync();

await client
    .For("Products")
    .Key(product)
    .LinkEntryAsync("Category", category);

Typed syntax

var category = await client
    .For<Categories>()
    .Set(new { CategoryName = "Test1" })
    .InsertEntryAsync();
var product = await client
    .For<Products>()
    .Set(new { ProductName = "Test2", CategoryID = 1 })
    .InsertEntryAsync();

await client
    .For<Products>()
    .Key(product)
    .LinkEntryAsync(x => x.Category, category);

Dynamic syntax

var x = ODataDynamic.Expression;
var category = await client
    .For(x.Categories)
    .Set(x.CategoryName = "Test1")
    .InsertEntryAsync();
var product = await client
    .For(x.Products)
    .Set(x.ProductName = "Test2", x.CategoryID = 1)
    .InsertEntryAsync();

await client
    .For(x.Products)
    .Key(product)
    .LinkEntryAsync(x.Category, category);

Request URI: PUT Products(80)/$links/Category
Request content:

<uri xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">Categories(11)</uri>

Unlink a product from a category

Untyped syntax

var category = await client
    .For("Categories")
    .Set(new { CategoryName = "Test6" })
    .InsertEntryAsync();
var product = await client
    .For("Products")
    .Set(new { ProductName = "Test7", Category = category })
    .InsertEntryAsync();

await client
    .For("Products")
    .Key(product)
    .UnlinkEntryAsync("Category");

Typed syntax

var category = await client
    .For<Categories>()
    .Set(new { CategoryName = "Test6" })
    .InsertEntryAsync();
var product = await client
    .For<Products>()
    .Set(new { ProductName = "Test7", Category = category })
    .InsertEntryAsync();

await client
    .For<Products>()
    .Key(product)
    .UnlinkEntryAsync(x => x.Category);

Dynamic syntax

var x = ODataFilter.Expression;
var category = await client
    .For(x.Categories)
    .Set(x.CategoryName = "Test6")
    .InsertEntryAsync();
var product = await client
    .For(x.Products)
    .Set(x.ProductName = "Test7", x.Category = category)
    .InsertEntryAsync();

await client
    .For(x.Products)
    .Key(product)
    .UnlinkEntryAsync(x.Category);

Request URI: DELETE Products(81)/$links/Category


Link a category to multiple products

Untyped syntax

var category = await client
    .For("Categories")
    .Set(new { CategoryName = "Test3" })
    .InsertEntryAsync();
var product1 = await client
    .For("Products")
    .Set(new { ProductName = "Test4", UnitPrice = 21m, CategoryID = 1 })
    .InsertEntryAsync();
var product2 = await client
    .For("Products")
    .Set(new { ProductName = "Test5", UnitPrice = 22m, CategoryID = 1 })
    .InsertEntryAsync();

await client
    .For("Categories")
    .Key(category)
    .LinkEntryAsync("Products", new[] { product1, product2 });

Typed syntax

var category = await client
    .For<Categories>()
    .Set(new { CategoryName = "Test3" })
    .InsertEntryAsync();
var product1 = await client
    .For<Products>()
    .Set(new { ProductName = "Test4", UnitPrice = 21m, CategoryID = 1 })
    .InsertEntryAsync();
var product2 = await client
    .For<Products>()
    .Set(new { ProductName = "Test5", UnitPrice = 22m, CategoryID = 1 })
    .InsertEntryAsync();

await client
    .For<Categories>()
    .Key(category)
    .LinkEntryasync(x => x.Products, new[] { product1, product2 });

Dynamic syntax

var x = ODataFilter.Expression;
var category = await client
    .For(x.Categories)
    .Set(x.CategoryName = "Test3")
    .InsertEntryAsync();
var product1 = await client
    .For(x.Products)
    .Set(x.ProductName = "Test4", x.UnitPrice = 21m, x.CategoryID = 1)
    .InsertEntryAsync();
var product2 = await client
    .For("Products")
    .Set(x.ProductName = "Test5", x.UnitPrice = 22m, x.CategoryID = 1)
    .InsertEntryAsync();

await client
    .For("Categories")
    .Key(category)
    .LinkEntryAsync(x.Products, new[] { product1, product2 });

Request URI: MERGE Categories(13)/$links/Products
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-15T14:45:21.7200000Z</updated>
  <author>
    <name />
  </author>
  <id />
  <content type="application/xml">
    <m:properties />
  </content>
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Products" type="application/atom+xml;type=Entry" title="Products" href="Products(110)" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Products" type="application/atom+xml;type=Entry" title="Products" href="Products(111)" />
</entry>

See also:
Adding entries with links
Updating entries with links
Modifying data