Preprocess Query and Include Related Entity
-
Sunday, March 04, 2012 8:10 AM
Is possible to add include extension before executing query.
I created CurrentEmployee query:
partial void CurrentEmployee_PreprocessQuery(ref IQueryable<Employee> query) { query = query.Where(i => i.UserName == this.Application.User.Name);
//query = query.Include("Location") ? }
and would like to include Location entity to which employee belong.
I can do that on this way:
DataWorkspace.AplicationData.Employees.Where(some condition).Include("Location").FirstOrDefault();But can I include Location entity before executing my custom query?
All Replies
-
Sunday, March 04, 2012 11:11 AM
No, I don't think that is possible as you would be changing the definition of the entity that was returned by the query. Add another query to get the Location for the Employee (by the Employee ID) and add that query to your screen. Tie the query parameter to the Employee ID field and bobs your uncle.
Simon Jones
If you found this post helpful, please "Vote as Helpful". If it actually answered your question, please remember to "Mark as Answer". This will help other people find answers to their problems more quickly.- Marked As Answer by ninoid Monday, March 05, 2012 6:43 AM
- Edited by Simon Jones [MSDL] Monday, March 05, 2012 8:16 AM Corrected word order
-
Sunday, March 04, 2012 6:55 PMModerator
ninoid,
It is not possible to use the "Include" operator from within PreprocessQuery. Only the caller that is executing the query can choose to include related entities. LightSwitch's design is that the code that is requesting the data picks the related information, instead of the defining query. This helps when processing the query results on the client because the client knows exactly the shape of the data, since it defined the shape when it issued the query. Ther server can't add or remove related entities because then the client would get confused.
You can use related entities in Where and OrderBy operators if you want. You just can't return related data if the client didn't ask for it.
So, like you said, you can add Includes from your code, or you can "Manage Included Data" in a Screen Collection Property's Query to define the shape of data being returned. (Note that if a related entity is shown on the screen, it will automatically get Included by LightSwitch.)
- Proposed As Answer by Eric ErhardtMicrosoft Employee, Moderator Sunday, March 04, 2012 6:55 PM
- Marked As Answer by ninoid Monday, March 05, 2012 6:43 AM
-
Monday, March 05, 2012 6:53 AM
Well, thanks people.
I was confused when i tried to include related data on server side so I ask question here.
On every screen I call current employee to find out who is user and to which Location belongs. When I trace EF to SQL queries (Sql profiler) I notice that
when I access to currentEmplyee.Location fires another query (which is another trip to sql server). So I want to include all needed data at same time.
I know that LS use lazy loading and I will respect that from now.
Eric gives us a good lesson to clarify this things :)...

