Telerik OpenAccess ORM

Telerik OpenAccess ORM Send comments on this topic.
Walkthrough: Filtering Data
Programmer's Guide > Quick-Start Scenarios > Silverlight > Silverlight 4 & 5 > WCF Data Services > Walkthrough: Filtering Data

Glossary Item Box

In this task you will learn how to query and filter the data returned by the WCF Data Service.

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

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.
    XAML Copy Code
    <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.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>
           
    <sdk: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.
    C# Copy Code
    using System;
    using System.Collections.ObjectModel;
    using System.Data.Services.Client;
    using System.Linq;
    using System.Windows.Controls;
    using OA.SL.WcfData.Demo.SofiaCarRentalWcfDataService;
    namespace OA.SL.WcfData.Demo
    {
       
    public partial class MainPage : UserControl
       {
           
    private SofiaCarRentalWcfDataService.SofiaCarRentalDbContext dataManager = new SofiaCarRentalWcfDataService.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 );
           }
       }
    }
    VB.NET Copy Code
    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))

        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.