Using the QueryExpression Class
![]() |
[Applies to: Microsoft Dynamics CRM 4.0]
Use the QueryExpression class to build complex queries for use with the RetrieveMultiple method or the RetrieveMultiple message.
The following table lists the properties that you set to create a query expression.
| Property | Description |
| EntityName | Specifies which kind of entity will be retrieved. A query expression can only retrieve a collection of one entity type. |
| ColumnSet | Specifies the set of attributes (columns) to retrieve. |
| Criteria | Specifies complex condition and logical filter expressions that filter the results of the query. |
| Distinct | Specifies whether the results of the query contain duplicate entity instances. |
| LinkEntities | Specifies the links between multiple entity types. |
| Orders | Specifies the order in which the entity instances are returned from the query. |
| PageInfo | Specifies the number of pages and the number of entity instances per page returned from the query. |
Example
This sample shows how to use a query expression to retrieve all accounts whose owner's last name is not Cannon.
[C#]
// Set up the CRM Service.
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = "AdventureWorksCycle";
CrmService service = new CrmService();
service.Url = ""http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Create a column set holding the names of the columns to be retrieved.
ColumnSet cols = new ColumnSet();
cols.Attributes = new string [] {"name", "accountid"};
// Create the ConditionExpression.
ConditionExpression condition = new ConditionExpression();
// Set the condition to be when the account owner's last name is not Cannon.
condition.AttributeName = "lastname";
condition.Operator = ConditionOperator.Equal;
condition.Values = new string [] {"Cannon"};
// Build the filter that is based on the condition.
FilterExpression filter = new FilterExpression();
filter.FilterOperator = LogicalOperator.And;
filter.Conditions = new ConditionExpression[] {condition};
// Create a LinkEntity to link the owner's information to the account.
LinkEntity link = new LinkEntity();
// Set the LinkEntity properties.
link.LinkCriteria = filter;
// Set the linking entity to account.
link.LinkFromEntityName = EntityName.account.ToString();
// Set the linking attribute to owninguser.
link.LinkFromAttributeName = "owninguser";
// The attribute being linked to is systemuserid.
link.LinkToAttributeName = "systemuserid";
// The entity being linked to is systemuser.
link.LinkToEntityName = EntityName.systemuser.ToString();
// Create an instance of the query expression class.
QueryExpression query = new QueryExpression();
// Set the query properties.
query.EntityName = EntityName.account.ToString();
query.ColumnSet = cols;
query.LinkEntities = new LinkEntity[] {link};
// Create the request.
RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();
// Set the request properties.
retrieve.Query = query;
// Execute the request.
RetrieveMultipleResponse retrieved2 = (RetrieveMultipleResponse) service.Execute(retrieve);
[Visual Basic .NET]
' Set up the CRM Service.
Dim token As New CrmAuthenticationToken()
token.AuthenticationType = 0
token.OrganizationName = "AdventureWorksCycle";
Dim service As New CrmService()
service.Url = ""http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials
' Create a column set holding the names of the columns to be retrieved.
Dim cols As New ColumnSet()
cols.Attributes = New String() {"name", "accountid"}
' Create the ConditionExpression.
Dim condition As New ConditionExpression()
' Set the condition to be when the account owner's last name is not Cannon.
condition.AttributeName = "lastname"
condition.[Operator] = ConditionOperator.NotEqual
condition.Values = New String() {"Cannon"}
' Build the filter that is based on the condition.
Dim filter As New FilterExpression()
filter.FilterOperator = LogicalOperator.And
filter.Conditions = New ConditionExpression() {condition}
' Create a LinkEntity to link the owner's information to the account.
Dim link As New LinkEntity()
' Set the LinkEntity properties.
link.LinkCriteria = filter
' Set the linking entity to account.
link.LinkFromEntityName = EntityName.account.ToString()
' Set the linking attribute to owninguser.
link.LinkFromAttributeName = "owninguser"
' The attribute being linked to is systemuserid.
link.LinkToAttributeName = "systemuserid"
' The entity being linked to is systemuser.
link.LinkToEntityName = EntityName.systemuser.ToString()
' Create the query expression.
Dim query As New QueryExpression()
' Set the query properties.
query.EntityName = EntityName.account.ToString()
query.ColumnSet = cols
query.LinkEntities = New LinkEntity() {link}
' Create the request.
Dim retrieve As New RetrieveMultipleRequest()
' Set the request properties.
retrieve.Query = query
' Execute the request.
Dim retrieved2 As RetrieveMultipleResponse = CType(service.Execute(retrieve), RetrieveMultipleResponse)
See Also
Other Resources
© 2010 Microsoft Corporation. All rights reserved.

