WCF RIA Service 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,

 

clip_image002

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,

 

clip_image004

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; 
 
        }
    }
}
 
 
 


		
About these ads

2 thoughts on “WCF RIA Service Adding Query Methods

  1. Pingback: Tweets that mention Learning WCF RIA Service Part #2: Adding Query Methods « DEBUG MODE…… -- Topsy.com

  2. Pingback: Monthly Report January 2010: Total Posts 19 « debug mode……

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s