-
Notifications
You must be signed in to change notification settings - Fork 221
Expanding results with linked entries
object edited this page Oct 9, 2012
·
10 revisions
Using Simple.Data With clause it's possible to expand results with associated data. Expanded entries must be defined as relationships in OData service schema.
var category = _db.Category
.WithProducts()
.FindByCategoryName("Beverages");
Assert.Equal("Beverages", category.CategoryName);
Assert.True(category.Products.Count > 0);
Request URI: GET Categories?$filter=CategoryName+eq+%27Beverages%27&$expand=Products&$top=1
var categories = _db.Category
.All()
.WithProducts()
.ToList();
Assert.True(categories.Count > 0);
Assert.True(categories[0].Products.Count > 0);
Request URI: GET Categories?expand=Products
var products = _db.Products
.All()
.WithCategory()
.ToList();
Assert.True(products.Count > 0);
Assert.Equal("Beverages", products[0].Category.CategoryName);
Request URI: GET Products?$expand=Category
var customer = _db.Customer
.WithOrders()
.Get("ALFKI");
Assert.Equal("ALFKI", customer.CustomerID);
Assert.True(customer.Orders.Count > 0);
Request URI: GET Customers(%27ALFKI%27)?$expand=Orders
var employees = _db.Employees
.All()
.WithSubordinates()
.ToList();
Assert.True(employees.Count > 0);
Assert.True(employees[0].Subordinates.Count == 0);
Assert.True(employees[1].Subordinates.Count > 0);
Request URI: GET Employees?$expand=Subordinates
var employee = _db.Employees
.WithSuperior()
.FindByFirstNameAndLastName("Nancy", "Davolio");
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