-
Notifications
You must be signed in to change notification settings - Fork 202
Expanding results with linked entries
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.
Find a category by name, expand with associated products:
Request URI: Categories?$filter=CategoryName+eq+%27Beverages%27&$expand=Products&$top=1
var category = _db.Category
.WithProducts()
.FindByCategoryName("Beverages");
Assert.Equal("Beverages", category.CategoryName);
Assert.True(category.Products.Count > 0);
Find all categories expanded with associated products (one-to-many relationship expansion):
Request URI: Categories?expand=Products
var categories = _db.Category
.All()
.WithProducts()
.ToList();
Assert.True(categories.Count > 0);
Assert.True(categories[0].Products.Count > 0);
Find all products expanded with linked categories (many-to-one relationship expansion):
Request URI: Products?$expand=Category
var products = _db.Products
.All()
.WithCategory()
.ToList();
Assert.True(products.Count > 0);
Assert.Equal("Beverages", products[0].Category.CategoryName);
Get a customer by key, expand with associated orders:
Request URI: Customers(%27ALFKI%27)?$expand=Orders
var customer = _db.Customer
.WithOrders()
.Get("ALFKI");
Assert.Equal("ALFKI", customer.CustomerID);
Assert.True(customer.Orders.Count > 0);
Find all employees, expand with associated subordinates:
Request URI: Employees?$expand=Subordinates
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);
Find an employee with it's superior:
Request URI: Employees?$filter=(FirstName+eq+%27Nancy%27+and+LastName+eq+%27Davolio%27)&$expand=Superior&$top=1
var employee = _db.Employees
.WithSuperior()
.FindByFirstNameAndLastName("Nancy", "Davolio");
Assert.NotNull(employee);
Assert.NotNull(employee.Superior);
See also:
Retrieving linked entries without fetching its owners
Retrieving data