Loading 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 retrieve related data by using WCF Data Service.
To display all related Car objects for a specific customer:
- In the client project, open MainPage.xaml.
- Add a ComboBox and 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 xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverlightOpenAccessIntegration.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <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" ItemsSource="{Binding ElementName=SelectedItem, Path=Cars}" /> </Grid> </UserControl>
Open the code-behind page for MainPage.xaml.
-
Add code to retrieve all related Car objects for a specific Customer.
using System; using System.Collections.ObjectModel; using System.Data.Services.Client; using System.Linq; using System.Windows.Controls; using SilverlightOpenAccessIntegration.SofiaCarRentalWcfDataService; namespace SilverlightOpenAccessIntegration { public partial class MainPage : UserControl{ private SofiaCarRentalDbContext dataManager = new SofiaCarRentalDbContext( new Uri( "SofiaCarRentalWCFDataService.svc", UriKind.Relative ) ); private ObservableCollection<Customer> dataSource = new ObservableCollection<Customer>(); public MainPage() { InitializeComponent(); this.CustomersCombo.ItemsSource = dataSource; DataServiceQuery<Customer> query = dataManager.Customers; query.BeginExecute( s => { DataServiceQuery<Customer> state = s.AsyncState as DataServiceQuery<Customer>; foreach ( var entity in state.EndExecute( s ) ) dataSource.Add( entity ); }, query ); } private void CustomersCombo_SelectionChanged( object sender, SelectionChangedEventArgs e ) { if ( CustomersCombo.SelectedIndex == -1 ) return; Customer selectedCustomer = this.CustomersCombo.SelectedItem as Customer; DataServiceQuery<RentalOrder> query = ( from ro in this.dataManager.RentalOrders.Expand( "Car" ) where ro.CustomerID == selectedCustomer.CustomerID select ro ) as DataServiceQuery<RentalOrder>; query.BeginExecute( s => { DataServiceQuery<RentalOrder> state = s.AsyncState as DataServiceQuery<RentalOrder>; ObservableCollection<Car> cars = new ObservableCollection<Car>(); this.CarsGrid.ItemsSource = cars; foreach ( var entity in state.EndExecute( s ) ) cars.Add( entity.Car ); }, query ); } } }
Imports System.Data.Services.Client Imports System.Collections.ObjectModel Imports SilverlightOpenAccessIntegration.SofiaCarRentalWcfDataService Partial Public Class MainPage Inherits UserControl Private dataManager As New SofiaCarRentalWcfDataService. SofiaCarRentalDbContext(New Uri("SofiaCarRentalWCFDataService.svc", _ UriKind.Relative)) Private dataSource As New ObservableCollection(Of Customer)() Public Sub New() InitializeComponent() Me.CustomersCombo.ItemsSource = dataSource Dim query As DataServiceQuery(Of Customer) = dataManager.Customers query.BeginExecute(Sub(s) Dim state As DataServiceQuery(Of Customer) = _ TryCast(s.AsyncState, DataServiceQuery(Of Customer)) For Each entity In state.EndExecute(s) dataSource.Add(entity) Next entity End Sub, query) End Sub Private Sub CustomersCombo_SelectionChanged(ByVal sender As Object, _ ByVal e As SelectionChangedEventArgs) If CustomersCombo.SelectedIndex = -1 Then Return End If Dim selectedCustomer As Customer = _ TryCast(Me.CustomersCombo.SelectedItem, Customer) Dim query As DataServiceQuery(Of RentalOrder) = TryCast(( From ro In Me.dataManager.RentalOrders.Expand("Car") Where ro.CustomerID = selectedCustomer.CustomerID Select ro), DataServiceQuery(Of RentalOrder)) query.BeginExecute(Sub(s) Dim state As DataServiceQuery(Of RentalOrder) = _ TryCast(s.AsyncState, DataServiceQuery(Of RentalOrder)) Dim cars As New ObservableCollection(Of Car)() Me.CarsGrid.ItemsSource = cars For Each entity In state.EndExecute(s) cars.Add(entity.Car) Next entity End Sub, query) End Sub End Class
-
Run the application. The following illustration shows a list of Cars filtered by Customer.