forked from simplefx/Simple.OData
-
Notifications
You must be signed in to change notification settings - Fork 200
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.
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);
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);
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
var categories = await client
.For("Categories")
.Expand("Products")
.FindEntriesAsync();
Assert.True(categories.Count > 0);
Assert.True((categories[0]["Products"] as IEnumerable<object>).Count() > 0);
var categories = await client
.For<Categories>()
.Expand<Products>()
.FindEntriesAsync();
Assert.True(categories.Count() > 0);
Assert.True(categories.First().Products.Count() > 0);
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
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"]);
var products = await client
.For<Products>()
.Expand(x => x.Category)
.FindEntriesAsync();
Assert.True(products.Count() > 0);
Assert.Equal("Beverages", products.First().Category.CategoryName);
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
var customer = await client
.For("Customers")
.Key("ALFKI")
.Expand("Orders")
.FindEntryAsync();
Assert.Equal("ALFKI", customer["CustomerID"]);
Assert.True(customer.Orders.Count > 0);
var customer = await client
.For<Customers>()
.Key("ALFKI")
.Expand<Orders>()
.FindEntryAsync();
Assert.Equal("ALFKI", customer.CustomerID);
Assert.True(customer.Orders.Count() > 0);
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
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);
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);
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
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"]);
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);
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