Learning WCF RIA Service Part #2: Adding Query Methods



Learning WCF RIA Service Part#1 can be read here

This is the second article of learning WCF RIA series. In this article we will see

  1. How to pass parameter in query
  2. How to return single object
  3. How to return multiple objects.
  4. How to call the query method with parameter.

In all we will add query methods in this article.

In our previous article, we fetched all the records;

Returning single object

Now let us make our query more particular. If we want to fetch details of a particular Person of given PersonID then
we need to modify. So to modify the query open DomainService1.cs and modify the query as below,

1.gif
  1. We pass the parameter Id and returning the Person objects.
  2. If query is retuning single object then we will have to make [Query (IsComposable=false)].

Now to call this service we need to call the method as below,

MainPage.xaml.cs

using System.Windows.Controls;
using RIA1.Web;
using System.ServiceModel.DomainServices.Client; 

namespace RIA1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            DomainService1 context = new DomainService1();
            LoadOperation<Person> loapPerson = context.Load(context.GetPersonByIdQuery(1));
            myGrid.ItemsSource = loapPerson.Entities; 

        }
    }
}

In above we are passing person id as 1.

Retuning List of objects

Now if we want to pass a parameter and expecting list of objects to return, then we need to modify
DomainService1.cs as below,

2.gif

And we will call above method as below,

MainPage.xaml.cs

using System.Windows.Controls;
using RIA1.Web;
using System.ServiceModel.DomainServices.Client; 

namespace RIA1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            DomainService1 context = new DomainService1();
            LoadOperation<Person> loapPerson = context.Load
            (context.GetPersonsbyStartNameQuery("b"));
            myGrid.ItemsSource = loapPerson.Entities; 

        }
    }
}

Up Next
    Ebook Download
    View all
    Learn
    View all