Learning WCF RIA Service Part #2: Adding Query Methods

No.of Views903
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  29 Jan 2011 20:01:02
Tag : WCF , REST Services
In this article we will see,How to pass parameter in query ,How to return single object ,How to return multiple objects.,How to call the query method with parameter.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

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;

Read first part of the article here

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,

 

Image Loading

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,

Image Loading

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.GetPersonsbyStart   NameQuery("b"));
            myGrid.ItemsSource = loapPerson.Entities; 
 
        }
    }
}

That's all. happy coding.

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top