Skip to content

Expanding results with linked entries

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

Using Expand clause it's possible to expand results with associated data. Expanded entries must be defined as relationships in OData service schema.


Find a category by name, expand with associated products

Untyped syntax

var category = await client
    .For("Categories")
    .Filter("CategoryName+eq+%27Beverages%27")
    .Expand("Products")
    .FindEntryAsync();
Assert.Equal("Beverages", category["CategoryName"]);
Assert.True((category.Products as IEnumerable<object>).Count() > 0);

Typed syntax

var category = await client
    .For<Categories>()
    .Filter(x => x.CategoryName == "Beverages")
    .Expand(x => x.Products)
    .FindEntryAsync();
Assert.Equal("Beverages", category.CategoryName);
Assert.True(category.Products.Count() > 0);

Dynamic syntax

var x = ODataFilter.Expression;
var category = await client
    .For(x.Categories)
    .Filter(x.CategoryName == "Beverages")
    .Expand(x.Products)
    .FindEntryAsync();
Assert.Equal("Beverages", category.CategoryName);
Assert.True((category.Products as IEnumerable<object>).Count() > 0);

Request URI: GET Categories?$filter=CategoryName+eq+%27Beverages%27&$expand=Products&$top=1


Find all categories expanded with associated products (one-to-many relationship expansion)

Untyped syntax

var categories = await client
    .For("Categories")
    .Expand("Products")
    .FindEntriesAsync();
Assert.True(categories.Count > 0);
Assert.True((categories[0]["Products"] as IEnumerable<object>).Count() > 0);

Typed syntax

var categories = await client
    .For<Categories>()
    .Expand<Products>()
    .FindEntriesAsync();
Assert.True(categories.Count() > 0);
Assert.True(categories.First().Products.Count() > 0);

Dynamic syntax

var x = ODataDynamic.Expression;
IEnumerable<dynamic> categories = await client
    .For(x.Categories)
    .Expand(x.Products)
    .FindEntriesAsync();
Assert.True(categories.Count() > 0);
Assert.True((categories.First().Products as IEnumerable<object>).Count() > 0);

Request URI: GET Categories?expand=Products


Find all products expanded with linked categories (many-to-one relationship expansion)

Untyped syntax

var products = await client
    .For("Products")
    .Expand("Category")
    .FindEntriesAsync();
Assert.True(products.Count > 0);
Assert.Equal("Beverages", (products[0]["Category"] as IDictionary<string, object>)["CategoryName"]);

Typed syntax

var products = await client
    .For<Products>()
    .Expand(x => x.Category)
    .FindEntriesAsync();
Assert.True(products.Count() > 0);
Assert.Equal("Beverages", products.First().Category.CategoryName);

Dynamic syntax

var x = ODataDynamic.Expression;
IEnumerable<dynamic> products = await client
    .For(x.Products)
    .Expand(x.Category)
    .FindEntriesAsync();
Assert.True(products.Count() > 0);
Assert.Equal("Beverages", products.First().Category.CategoryName);

Request URI: GET Products?$expand=Category


Get a customer by key, expand with associated orders

Untyped syntax

var customer = await client
    .For("Customers")
    .Key("ALFKI")
    .Expand("Orders")
    .FindEntryAsync();
Assert.Equal("ALFKI", customer["CustomerID"]);
Assert.True(customer.Orders.Count > 0);

Typed syntax

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

Dynamic syntax

var x = ODataDynamic.Expression;
var customer = await client
    .For(x.Customers)
    .Key("ALFKI")
    .Expand(x.Orders)
    .FindEntryAsync();
Assert.Equal("ALFKI", customer.CustomerID);
Assert.True((customer.Orders as IEnumerable<object>).Count() > 0);

Request URI: GET Customers(%27ALFKI%27)?$expand=Orders


Find all employees, expand with associated subordinates

Untyped syntax

var employees = await client
    .For("Employees")
    .Expand("Subordinates")
    .FindEntriesAsync();
Assert.True(employees.Count > 0);
Assert.True((employees[0]["Subordinates"] as IEnumerable<object>).Count() == 0);
Assert.True((employees[0]["Subordinates"] as IEnumerable<object>).Count() > 0);

Typed syntax

var employees = await client
    .For<Employees>()
    .Expand(x => x.Subordinates)
    .FindEntriesAsync();
Assert.True(employees.Count() > 0);
Assert.True(employees.First().Subordinates.Count() == 0);
Assert.True(employees.First().Subordinates.Count() > 0);

Dynamic syntax

var x = ODataDynamic.Expression;
IEnumerable<dynamic> employees = await client
    .For(x.Employees)
    .Expand(x.Subordinates)
    .FindEntriesAsync();
Assert.True(employees.Count > 0);
Assert.True((employees.First().Subordinates as IEnumerable<object>).Count() == 0);
Assert.True((employees.First().Subordinates as IEnumerable<object>).Count() > 0);

Request URI: GET Employees?$expand=Subordinates


Find an employee with it's superior

Untyped syntax

var employee = await client
    .For("Employees")
    .Filter("(FirstName+eq+%27Nancy%27+and+LastName+eq+%27Davolio%27)")
    .Expand("Superior")
    .FindEntriesAsync();
Assert.NotNull(employee);
Assert.NotNull(employee["Superior"]);

Typed syntax

var employee = await client
    .For<Employees>()
    .Filter(x => x.FirstName == "Nancy" && x.LastName == "Davolio")
    .Expand(x => x.Superior)
    .FindEntriesAsync();
Assert.NotNull(employee);
Assert.NotNull(employee.Superior);

Dynamic syntax

var x = ODataFilter.Expression;
var employee = await client
    .For(x.Employees)
    .Filter(x.FirstName == "Nancy" && x.LastName == "Davolio")
    .Expand(x.Superior)
    .FindEntriesAsync();
Assert.NotNull(employee);
Assert.NotNull(employee.Superior);

Request URI: GET Employees?$filter=(FirstName+eq+%27Nancy%27+and+LastName+eq+%27Davolio%27)&$expand=Superior&$top=1


See also:
Retrieving linked entries without fetching its owners
Retrieving data

Clone this wiki locally