How to: Load Related Data
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
In this walkthrough, you will learn how to add a query method in the Domain Service to return related data.
This topic is based on the WCF RIA Services example solution from the Walkthrough: Creating a RIA Services Solution.
Editing a Domain Service Query to Return Related Data
In order to return related data you should add a custom query method in the partial domain service. The process of adding query methods is explained in the Walkthrough: Adding Query Methods. The following example shows you how to return all cars bought by a specific customer.
To add a query method that accepts a parameter and returns related data:
- Open the solution that exposes data from the Customer table.
- In the server project, open the partial domain service class that exposes data from the Customer table.
-
Add a query method that accepts a string parameter and returns all Car objects, which are bought by the Customer.
public partial class SofiaCarRentalDomainService { public IQueryable<Car> GetCarByCustomerName( string customerName ) { var query = from c in this.DataContext.Cars from ro in this.DataContext.RentalOrders where ro.CarID == c.CarID && ro.Customer.FullName == customerName select c; return query; } }
Partial Public Class SofiaCarRentalDomainService Public Function GetCarByCustomerName(ByVal customerName As String) As IQueryable(Of Car) Dim query = From c In Me.DataContext.Cars, ro In Me.DataContext.RentalOrders Where ro.CarID = c.CarID AndAlso ro.Customer.FullName = customerName Select c Return query End Function End Class
To display the results of those query methods in the client project:
- In the client project, open MainPage.xaml.
- Add a ComboBox and a DataGrid to the user interface so that the user can filter Car records by the full name of the Customer.
-
The following XAML shows a complete layout.
<UserControl x:Class="OA.SL.RIA.Demo.MainPage" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"> <Grid x:Name="LayoutRoot" Background="White"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition /> </Grid.RowDefinitions> <ComboBox x:Name="CustomersCombo" ItemsSource="{Binding}" Width="200" Height="30" Margin="5,5,5,0" HorizontalAlignment="Left" DisplayMemberPath="FullName" SelectionChanged="CustomersCombo_SelectionChanged"/> <data:DataGrid x:Name="CarsGrid" Grid.Row="1" /> </Grid> </UserControl>
Open the code-behind page for MainPage.xaml.
-
Add code to retrieve query results.
using System.ServiceModel.DomainServices.Client; using System.Windows.Controls; using OA.SL.RIA.Demo.Web; using SofiaCarRentalEntityDiagramsModel; namespace OA.SL.RIA.Demo { public partial class MainPage : UserControl { private SofiaCarRentalDomainContext dbContext = new SofiaCarRentalDomainContext(); public MainPage() { InitializeComponent(); LoadOperation<Customer> loadOperation = this.dbContext .Load( this.dbContext.GetCustomersQuery() ); this.DataContext = loadOperation.Entities; } private void CustomersCombo_SelectionChanged( object sender, SelectionChangedEventArgs e ) { if ( this.CustomersCombo.SelectedItem == null ) return; Customer customer = this.CustomersCombo.SelectedItem as Customer; LoadOperation<Car> loadOperation = this.dbContext .Load<Car>( this.dbContext.GetCarByCustomerNameQuery( customer.FullName ) ); this.CarsGrid.ItemsSource = loadOperation.Entities; } } }
Imports System.ServiceModel.DomainServices.Client Imports OA.SL.RIA.Demo.Web Imports OA.SL.RIA.Demo.Web.OA.SL.RIA.Demo.Web Partial Public Class MainPage Inherits UserControl Private dbContext As New SofiaCarRentalDomainContext() Public Sub New() InitializeComponent() Dim loadOperation_Renamed As LoadOperation(Of Customer) = Me.dbContext.Load(Me.dbContext.GetCustomersQuery()) Me.DataContext = loadOperation_Renamed.Entities End Sub Private Sub CustomersCombo_SelectionChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs) If Me.CustomersCombo.SelectedItem Is Nothing Then Return End If Dim customer_Renamed As Customer = TryCast(Me.CustomersCombo.SelectedItem, Customer) Dim loadOperation_Renamed As LoadOperation(Of Car) = Me.dbContext_ .Load(Of Car)(Me.dbContext.GetCarByCustomerNameQuery(customer_Renamed.FullName)) Me.CarsGrid.ItemsSource = loadOperation_Renamed.Entities End Sub End Class
-
Run the application. The following illustration shows a list of Cars filtered by the Customer.