Reputation:
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.
Upvotes: 1
Views: 821
Reputation: 20800
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 :(
Upvotes: 0
Reputation: 77580
First()
should throw an exception if the result set is empty - are you sure the query is even executing?
Upvotes: 0
Reputation:
Sorry was supposed to be
var qry = (from p in svcContext.Products where p.ProductName == "Chair" && p.Colour == 1 select p) as DataServiceQuery< Product >;
Upvotes: 0
Reputation: 11361
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 ?
Upvotes: 1