Skip to main content
Home Forums Silverlight Programming WCF RIA Services query question
7 replies. Latest Post by samw on June 30, 2009.
(0)
samw
Member
67 points
124 Posts
06-30-2009 1:45 AM |
I dont understand the communication process between client and server when a query is run. Here is my query that should return no rows:
public FeedbackPage() { InitializeComponent(); PersonalWebsiteDomainContext context = new PersonalWebsiteDomainContext(); this.DataContext = context; SamsDataGrid.ItemsSource = context.Feedbacks; var query = context.Feedbacks.Where(f => false).AsQueryable(); // should return no rows context.LoadFeedback(query, null);
I put a breakpoint on the server and the menthod that is running is:
public IQueryable GetFeedback() { return this.Context.Feedback; }
MarkMonster
Contributor
5220 points
1,046 Posts
06-30-2009 6:55 AM |
I'm not 100% sure, but. GetFeedback() returns IQueryable as well, so no real data is returned in the server method. The evaluation of the Linq-query is probably done at RIA Services framework level code.
kylemc
Participant
1482 points
255 Posts
06-30-2009 10:30 AM |
If you're not seeing the results you expect, it may be that you're putting the query together incorrectly. Typically you would use the query root that corresponds to the query you're performing.
context.LoadFeedback(context.FeedbackQueryRoot.Where(f => false), null)
This will still call the GetFeedback method on the server. However, your query will also have been serialized and will be applied to IQueryable returned from the GetFeedback method. The resulting queryable (presumably empty) will be returned back to the client.
Kyle
06-30-2009 11:28 AM |
>>applied to IQueryable returned from the GetFeedback method
Sorry, but I don't know what this means.
Should I be writing my queries on the server side and call them by name? I don't really want to touch the DomainService class generated by the RIA wizard. For some reason I'm thinking in a real application I may want to generate that class more than once.
Can one of you show me a quick example of how this should be done?
Thanks for the help.
Sam
ColinBlair
6579 points
1,291 Posts
06-30-2009 12:27 PM |
I think the other two answers are assuming that you aren't getting the results you expected (i.e. you got rows back at the client) while you never said that. All that you said was you didn't understand why the GetFeedback method was still being called and that you didn't understand how it was supposed to work.
The GetFeedback method is returning back the IQueryable this.Context.Feedback. It is important to understand that this is not going out to the database and getting all of the Feedback rows, it is just returning a query that could return all of the feedback rows if left as is. Within RIA Services that IQueryable is then combined with the query from the client Where(f => false) creating the combined query this.Context.Feedback.Where(f=>false) which is then executed against the model and the results (or lack of results in this case) are returned to the client.
In your latest message you said you didn't want to edit the DomainService yourself. This is the exact reaction I was afraid of when the wizard first showed up in RIA Services. The wizard should not be treated as a crutch, it is a useful tool that can quickly generate a bunch of code for us but it was called a wizard for a reason. It is not designed for code maintenance, just initial generation like the templates we use to create our projects. You are supposed to be editing the DomainService yourself, that is why it exists, that is why you would choose to use the DomainService instead of the DataService.
06-30-2009 1:03 PM |
>> I think the other two answers are assuming that you aren't getting the results you expected (i.e. you got rows back at the client) while you never said that. All that you said was you didn't understand why the GetFeedback method was still being called and that you didn't understand how it was supposed to work.
No, I'm not getting the result I expect. The query is returning all rows in the table, I expect no rows.
>>Within RIA Services that IQueryable is then combined with the query from the client Where(f => false) creating the combined query
>>it is just returning a query that could return all of the feedback rows if left as is.
I understand you to say there is a default query on the server that is just "true" (all rows) and it is being combined with my query (concatenated with logical AND I presume?), which is just "false", so after it's all said and done I should be running a query like Feedbacks.Where(true AND false) which should return no rows.
Sorry if this is repetitive, but can one of you show me a quick example of how this should be done?
This is what I was using.
Thanks.
mathewc
614 points
111 Posts
06-30-2009 9:35 PM |
You should use the query root we codegen for you as Kyle mentioned. To that you should append your query operators, and things should work as you expect.
Here's what's happening: you're passing in a query like ctxt.Entities.Where(p => false).AsQueryable() to the load method. The way our query serializer works is it walks the expression tree translating query operators until it hits the "query root". In your case, that means no operators are translated since your where operator was specified before the AsQueryable call. You can verify this by using your favorite tool to inspect the URL we're generating - I bet it has an empty query. So use the query root and form your query like Context.EntitiesQuery.Where(p => false) and you should get your expected results. Note that in the upcoming July CTP these "query roots" will go away and so will this issue - you'll use a new EntityQuery type.
06-30-2009 11:20 PM |
OK I got it. Thanks.
context.Entities.AsQueryable().Where(yada);