views:

363

answers:

4

Hi Guys,

Trying my hand at ADO.Net data services. All the examples shows how to retrieve lists but how would you go about retrieving a single value? e.g. Product X's Price.

Here is the LINQ query i use:

var qry = (from p in svcContext.Products where p.ProductName == "Chair" && p.Colour == 1 select c) as DataServiceQuery;

Product returnedProd;

qry.BeginExecute( (pr) => returnedProd = qry.EndExecute(pr).First(), null);

Here i try to retrieve the product and load it into a local variable, but the local var stays null.

Pretty sure, i'm doing it completely wrong :)...any help would be greatly appreciated.

+1  A: 

It's not suppose to be

var qry = (from p in svcContext.Products where p.ProductName == "Chair" && p.Colour == 1 select p) where did you declare the c ?

Cédric Boivin
A: 

Sorry was supposed to be

var qry = (from p in svcContext.Products where p.ProductName == "Chair" && p.Colour == 1 select p) as DataServiceQuery< Product >;

A: 

First() should throw an exception if the result set is empty - are you sure the query is even executing?

dahlbyk
A: 

You are not the first to get hit by the asynchronous nature of all silverlight outgoing requests.

In the lambda expression

(pr) => returnedProd = qry.EndExecute(pr).First()

you capture the local variable returnedProd but usually the thread that will spin off AFTER BeginExecute has been called will be too late. It will probably executed after the execution goes out of scope of the current method and the variable will be lost.

The solution is to use effectively the "returnedProd" to populate the UI or whatever you need to do IN the lambda expression. Something like :

(pr) => {
    returnedProd = qry.EndExecute(pr).First();
    MessageBox.Show("Retrieved record" + returnedProd.Id);
}

Otherwise useful answer for the community, I wish I had one a few weeks ago :(

Andrei Rinea
try this little piece of code and let us know if at least in the MessageBox.Show you get the desired data..
Andrei Rinea