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

Filtering 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 task you will learn how to query and filter the data returned by the WCF Data Service.

To filter the data returned by the WCF Data Service:

  1. In the client project, open MainPage.xaml.
  2. Add two TextBox controls and two Button controls so that the user can filter customer records by the ID or the first letter of the full name. Attach to the Click event of the Button controls.
  3. The following XAML shows a complete layout along with the existing DataGrid.

    <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.ColumnDefinitions>
               <ColumnDefinition></ColumnDefinition>
               <ColumnDefinition></ColumnDefinition>
           </Grid.ColumnDefinitions>
           <Grid.RowDefinitions>
               <RowDefinition Height="25"></RowDefinition>
               <RowDefinition></RowDefinition>
           </Grid.RowDefinitions>
           <StackPanel Orientation="Horizontal"
                       Grid.Row="0"
                       Grid.Column="0">
               <TextBlock Text="search by id: "></TextBlock>
               <TextBox Name="IDValue"
                        Width="50"></TextBox>
               <Button Name="IDButton"
                       Click="IDButton_Click"
                       Content="Submit"></Button>
           </StackPanel>
           <StackPanel Orientation="Horizontal"
                       Grid.Row="0"
                       Grid.Column="1">
               <TextBlock Text="search by name: "></TextBlock>
               <TextBox Name="LetterValue"
                        Width="30"></TextBox>
               <Button Name="LetterButton"
                       Click="LetterButton_Click"
                       Content="Submit"></Button>
           </StackPanel>
           <data:DataGrid Name="CustomersGrid"
                          Grid.Row="1"
                          Grid.Column="0"
                          Grid.ColumnSpan="2" />
       </Grid>
    </UserControl>
    
  4. Open the code-behind page for MainPage.xaml.

  5. Add code to retrieve query results based on the user input.

    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 ) );
    
           public MainPage()
           {
               InitializeComponent();
           }
    
           private void IDButton_Click( object sender, System.Windows.RoutedEventArgs e )
           {
               DataServiceQuery<Customer> query = ( from c in dataManager.Customers
                        where c.CustomerID == Convert.ToInt32( this.IDValue.Text )
                        select c ) as DataServiceQuery<Customer>;
               query.BeginExecute( s =>
               {
                   DataServiceQuery<Customer> state = s.AsyncState as DataServiceQuery<Customer>;
                   ObservableCollection<Customer> data = new ObservableCollection<Customer>();
                   foreach ( var entity in state.EndExecute( s ) )
                       data.Add( entity );
                   this.CustomersGrid.ItemsSource = data;
               }, query );
           }
    
           private void LetterButton_Click( object sender, System.Windows.RoutedEventArgs e )
           {
               DataServiceQuery<Customer> query = ( from c in dataManager.Customers
                        where c.FullName.StartsWith( this.LetterValue.Text ) == true
                        select c ) as DataServiceQuery<Customer>;
               query.BeginExecute( s =>
               {
                   DataServiceQuery<Customer> state = s.AsyncState as DataServiceQuery<Customer>;
                   ObservableCollection<Customer> data = new ObservableCollection<Customer>();
                   foreach ( var entity in state.EndExecute( s ) )
                       data.Add( entity );
                   this.CustomersGrid.ItemsSource = data;
               }, 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))
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Private Sub IDButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
            Dim query As DataServiceQuery(Of Customer) = TryCast((
                From c In dataManager.Customers
                Where c.CustomerID = Convert.ToInt32(Me.IDValue.Text)
                Select c), DataServiceQuery(Of Customer))
            query.BeginExecute(Sub(s)
                   Dim state As DataServiceQuery(Of Customer) = TryCast(s.AsyncState, DataServiceQuery(Of Customer))
                   Dim data As New ObservableCollection(Of Customer)()
                   For Each entity In state.EndExecute(s)
                       data.Add(entity)
                   Next entity
                   Me.CustomersGrid.ItemsSource = data
               End Sub, query)
        End Sub
    
        Private Sub LetterButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
            Dim query As DataServiceQuery(Of Customer) = TryCast((
                From c In dataManager.Customers
                Where c.FullName.StartsWith(Me.LetterValue.Text) = True
                Select c), DataServiceQuery(Of Customer))
            query.BeginExecute(Sub(s)
                   Dim state As DataServiceQuery(Of Customer) = TryCast(s.AsyncState, DataServiceQuery(Of Customer))
                   Dim data As New ObservableCollection(Of Customer)()
                   For Each entity In state.EndExecute(s)
                       data.Add(entity)
                   Next entity
                   Me.CustomersGrid.ItemsSource = data
               End Sub, query)
        End Sub
    End Class
    
  6. Run the application. The following illustration shows a list of customers filtered by their id.