locked
_PreprocessQuery() failed to return programmatically results. RRS feed

  • Question

  • I have query as follow

            partial void Item3_PreprocessQuery(ref IQueryable<Table1Item> query)
            {
                Table1Item item3 = this.DataWorkspace.ApplicationData.Table1Items_Single(3);
                query = (new List<Table1Item>() { item3 }).AsQueryable();
            }

    But the return results seems to list every record in the Table1Item items.

    I am using Visual studio 2013 with update 2

    Any help will be appreciated


    jl

    Tuesday, July 1, 2014 12:43 AM

Answers

  • The PreprocessQuery method is intended for applying additional filtering and/or sorting on an entity. It is useful where the filtering you want to do is not possible using the Query Designer - EG where you need to add an Any clause to return entities where their children match some criteria. (The Query Designer will only allow you to look at the current entity and its ancestors.)

    You don't have to use Lambda expressions. You could write Linq queries, based on the query parameter or not. You could completely replace the query that is passed to you but it is usually easier to start with that as a basis on which to build. Search in this forum for "PreprocessQuery" and you'll find plenty of examples where people have done all of these.

    The query you return will be passed up to the data source and executed there. IE if your data is in SQL Server, the query is transformed into an SQL Select statement and actually executes on SQL Server. So, you cannot use PreprocessQuery to change the type of the entities returned or otherwise affect the results, for instance by adding "virtual" entities. That is where I think the example in your original posting falls down. It contains no filter or sorting criteria to translate into SQL WHERE and ORDER BY clauses and so just returns all the rows in the table.


    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 Angie Xu Monday, July 7, 2014 2:16 AM
    Wednesday, July 2, 2014 6:57 AM

All replies

  • You don't need to write a query just to return an item by its ID.

    Just use the TableName_Single() or the TableName_SingleOrDefault() methods in your code directly.

    Using _SingleOrDefault() is nearly always preferable as it doesn't error if the item isn't found. It just returns null (Nothing).

    When you do need to write code in the PreprocessQuery event, it is better to use a Lambda syntax using the query you are passed as a firm foundation on which to build and Intellisense to help you get it right.

    EG

    query = query.Where(c => c.Name = NameParameter);
    Completely replacing the query with a new one of your own is much more prone to errors.


    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.

    Tuesday, July 1, 2014 7:24 AM
  • Hi EG

    Thanks for the reply. I am aware that I can use  _SingleOrDefault() to return item by its Id. The sample is an example of programmatically populating return reults. The question seems for MS guys to answer if the LS support this functionality. If not, it should be documented. To me is seems an undesired feature.

    JL


    jl

    Tuesday, July 1, 2014 10:44 PM
  • Perhaps you could explain further what you mean by "programmatically populating return results".

    I'm not sure what you are trying to achieve but if you use the lambda format I mentioned you are more likely to get correct results.


    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.

    Tuesday, July 1, 2014 10:54 PM
  • Hi 

    Thanks for the quick reply. What I mean programatically populating return results is that inside the _PreprocessQuery() method, programmer write code that determine what records will be included. For example. if you have various sources of data that dictate which entity should be included, lambda might not appropriate.

    My point is the if SL can only process lambda expression based on query parameter, it should be documented. From the generated method definition _PreprocessQuery(ref IQueryable<Item> query), it is not clear what the limitation it is

    Sorry for the lack of clarity of issues I posted for this issue. 


    jl

    Wednesday, July 2, 2014 12:58 AM
  • The PreprocessQuery method is intended for applying additional filtering and/or sorting on an entity. It is useful where the filtering you want to do is not possible using the Query Designer - EG where you need to add an Any clause to return entities where their children match some criteria. (The Query Designer will only allow you to look at the current entity and its ancestors.)

    You don't have to use Lambda expressions. You could write Linq queries, based on the query parameter or not. You could completely replace the query that is passed to you but it is usually easier to start with that as a basis on which to build. Search in this forum for "PreprocessQuery" and you'll find plenty of examples where people have done all of these.

    The query you return will be passed up to the data source and executed there. IE if your data is in SQL Server, the query is transformed into an SQL Select statement and actually executes on SQL Server. So, you cannot use PreprocessQuery to change the type of the entities returned or otherwise affect the results, for instance by adding "virtual" entities. That is where I think the example in your original posting falls down. It contains no filter or sorting criteria to translate into SQL WHERE and ORDER BY clauses and so just returns all the rows in the table.


    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 Angie Xu Monday, July 7, 2014 2:16 AM
    Wednesday, July 2, 2014 6:57 AM