locked
QueryByAttribute Vs QueryExpression - Whats the difference RRS feed

  • Question

  • I've built a set of helper routines to process RetrieveMultiple requests. These expect a QueryByAttribute query as a parameter before executing the request.

    I've just built a complex QueryExpression that uses a double link to get user roles. When I tried to pass this query to my execution routine it didn't like it because it was expecting QueryByAttribute and I sent it QueryExpression.

    I scratched my head for a minute, then realised I didn't actually know what the difference was between these queries, and why I've ended up using both types.

    Can somebody explain why they differ and which is best for what?

    Many thanks

    Steve
    Monday, June 22, 2009 4:27 PM

Answers

  • The biggest difference I have found is that queryexpressions can link multiple entities and allow for null values and more logical comparisons.  While both could retrieve a list of active accounts, only the query expression could retrieve active accounts created after certain date, or where a particular field is null.

    To execute a queryexpression, set the query property of the retrievemultiplerequest message.  The retrievemultiplerequest message can handle either querybyattribute or queryexpression and can return dynamic entities.
    • Edited by Richard M. Riddle Monday, June 22, 2009 4:45 PM added more info
    • Marked as answer by lemonje Tuesday, June 23, 2009 9:27 AM
    Monday, June 22, 2009 4:42 PM
  • Hi,
    Just to add,

    QueryByAttribute

    can be used when our query is very simple i.e. multiple equals conditions that are combined using AND. However it doesn’t support OR.

    QueryByAttribute myQuery=new QueryByAttribute();
    myQuery.ColumnSet=new AllColumns();
    myQuery.EntityName=EntityName.opportunity.ToString();
    
        // Find all the opportunities with topic name test1 and city as Redmond
    myQuery.Attributes=new string[]{”name”,”address1_city”};
    myQuery.Values=new string[]{”Test1″,”Redmond”};
    BusinessEntityCollection retrieved=service.RetrieveMultiple(myQuery); 
    The same thing could have taken many more lines of code for QueryExpression.

    Regards,

    Nishant Rana

     

     



    http://nishantrana.wordpress.com
    • Marked as answer by lemonje Tuesday, June 23, 2009 9:27 AM
    Tuesday, June 23, 2009 6:37 AM

All replies

  • The biggest difference I have found is that queryexpressions can link multiple entities and allow for null values and more logical comparisons.  While both could retrieve a list of active accounts, only the query expression could retrieve active accounts created after certain date, or where a particular field is null.

    To execute a queryexpression, set the query property of the retrievemultiplerequest message.  The retrievemultiplerequest message can handle either querybyattribute or queryexpression and can return dynamic entities.
    • Edited by Richard M. Riddle Monday, June 22, 2009 4:45 PM added more info
    • Marked as answer by lemonje Tuesday, June 23, 2009 9:27 AM
    Monday, June 22, 2009 4:42 PM
  • Hi,
    Just to add,

    QueryByAttribute

    can be used when our query is very simple i.e. multiple equals conditions that are combined using AND. However it doesn’t support OR.

    QueryByAttribute myQuery=new QueryByAttribute();
    myQuery.ColumnSet=new AllColumns();
    myQuery.EntityName=EntityName.opportunity.ToString();
    
        // Find all the opportunities with topic name test1 and city as Redmond
    myQuery.Attributes=new string[]{”name”,”address1_city”};
    myQuery.Values=new string[]{”Test1″,”Redmond”};
    BusinessEntityCollection retrieved=service.RetrieveMultiple(myQuery); 
    The same thing could have taken many more lines of code for QueryExpression.

    Regards,

    Nishant Rana

     

     



    http://nishantrana.wordpress.com
    • Marked as answer by lemonje Tuesday, June 23, 2009 9:27 AM
    Tuesday, June 23, 2009 6:37 AM
  • Thanks chaps for the explanation. I've now changed my helper routine to accept QueryBase as a parameter so that I can send it either.
    Cheers

    Steve
    Tuesday, June 23, 2009 9:26 AM