Retrieving linked entries without fetching its owners

object edited this page Oct 9, 2012 · 6 revisions

In addition to expanding results with associated data it's possible to navigate to linked entries directly, without fetching it's relationship principle. The relationship must be defined in OData service schema.


Find all products associated with a category with the given name

IEnumerable<dynamic> products = _db.Products
    .FindAll(_db.Products.Category.CategoryName == "Beverages");
Assert.NotEmpty(products);

Request URI: GET Products?$filter=Category%2fCategoryName+eq+%27Beverages%27


Find all employees for a superior with ghe given name

IEnumerable<dynamic> employees = _db.Employees
    .FindAll(_db.Employees.Superior.FirstName == "Andrew" && _db.Employees.Superior.LastName == "Fuller");
Assert.NotEmpty(employees);

Request URI: GET Employees?$filter=Superior/FirstName+eq+%27Nancy%27 and Superior/LastName+eq+%27Davolio%27


Find all orders for an employee with the given name

IEnumerable<dynamic> orders = _db.Orders
    .FindAll(_db.Orders.Employee.FirstName == "Andrew" && _db.Orders.Employee.LastName == "Fuller");
Assert.NotEmpty(orders);

Request URI: GET Orders?$filter=Employee/FirstName+eq+%27Andrew%27 and Employee/LastName+eq+%27Fuller%27


Find all orders for a customer identified by key

IEnumerable<dynamic> orders = _db.Customers
    .Orders
    .FindAll(_db.Customers.CustomerID == "ALFKI");
Assert.NotEmpty(orders);

Request URI: GET Customers('ALFKI')/Orders


Find all order details for an order identified by key

IEnumerable<dynamic> orderDetails = _db.Orders
    .OrderDetails
    .FindAll(_db.Orders.OrderID == 10952);
Assert.NotEmpty(orderDetails);

Request URI: GET Orders(10952)/OrderDetails


Find all employee's subordinates

IEnumerable<dynamic> subordinates = _db.Employees
    .Subordinates
    .FindAll(_db.Employees.EmployeeID == 2);
Assert.NotEmpty(subordinates);

Request URI: GET Employees(1)/Subordinates


Find a superior for an employee identified by key

var superior = _db.Employees
    .Superior
    .Get(1);
Assert.NotNull(superior);

Request URI: GET Employees(1)/Superior


Find a superior for an employee identified by EmployeeID

var superior = _db.Employees
    .Superior
    .Find(_db.Employees.EmployeeID == 1);
Assert.Equal(2, superior.EmployeeID);

Request URI: GET Employees(1)/Superior


Find all superiors (result will contain a single row) for an employee identified by EmployeeID

IEnumerable<dynamic> superiors = _db.Employees
    .Superior
    .FindAll(_db.Employees.EmployeeID == 1);
Assert.NotEmpty(superiors);

Request URI: GET Employees(1)/Superior


See also:
Expanding results with linked entries
Retrieving data