Data Access has been discontinued. Please refer to this page for more information.

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.

This topic is based on the WCF Data Services example solution from the Walkthrough: Creating a WCF Data Services Solution.

To display all related Car objects for a specific customer:

  1. In the client project, open MainPage.xaml.
  2. Add a ComboBox and DataGrid to the user interface so that the user can filter Car records by the full name of the Customer.
  3. The following XAML shows a complete layout.

    <UserControl x:Class="OA.SL.WcfData.Demo.MainPage"
       xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  
       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">
       <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" />
           <sdk:DataGrid x:Name="CarsGrid"
                          Grid.Row="1"
                          ItemsSource="{Binding ElementName=SelectedItem, Path=Cars}"/>
       </Grid>
    </UserControl>
    
  4. Open the code-behind page for MainPage.xaml.

  5. 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.Windows.Controls;
    using OA.SL.WcfData.Demo.SofiaCarRentalWcfDataService;
    using System.Linq;
    namespace OA.SL.WcfData.Demo
    {
       public partial class MainPage : UserControl
       {
           private SofiaCarRentalWcfDataService.SofiaCarRentalDbContext dataManager = 
               new SofiaCarRentalWcfDataService.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 OA.SL.WcfData.Demo.SofiaCarRentalWCFDataService
    Imports System.Collections.ObjectModel
    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
    
  6. Run the application. The following illustration shows a list of Cars filtered by Customer.