Use the QueryExpression class
Applies To: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online
In Microsoft Dynamics 365 and Microsoft Dynamics 365 (online), you can use the QueryExpression class to build complex queries for use with the IOrganizationService.RetrieveMultiple method or the RetrieveMultipleRequest message. You can set query parameters to the QueryExpression by using the ConditionExpression, ColumnSet, and FilterExpression classes.
The QueryExpression class lets you create complex queries. The QueryByAttribute class is designed to be a simple way to search for entities where attributes match specified values.
The following table lists the properties that you set to create a query expression.
Property |
Description |
---|---|
Specifies which type of entity will be retrieved. A query expression can only retrieve a collection of one entity type. |
|
Specifies the set of attributes (columns) to retrieve. |
|
Specifies complex conditional and logical filter expressions that filter the results of the query. |
|
Specifies whether the results of the query contain duplicate records. |
|
Specifies the links between multiple entity types. |
|
Specifies the order in which the records are returned from the query. |
|
Specifies the number of pages and the number of records per page returned from the query. |
Record count
To find out how many records the query returned, set the ReturnTotalRecordCount property to true before executing the query. When you do this, the TotalRecordCount will be set. Otherwise, this value will be -1.
Example
The following sample shows how to use the QueryExpression class.
// Query using ConditionExpression and FilterExpression
ConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = "lastname";
condition1.Operator = ConditionOperator.Equal;
condition1.Values.Add("Brown");
FilterExpression filter1 = new FilterExpression();
filter1.Conditions.Add(condition1);
QueryExpression query = new QueryExpression("contact");
query.ColumnSet.AddColumns("firstname", "lastname");
query.Criteria.AddFilter(filter1);
EntityCollection result1 = _serviceProxy.RetrieveMultiple(query);
Console.WriteLine();Console.WriteLine("Query using Query Expression with ConditionExpression and FilterExpression");
Console.WriteLine("---------------------------------------");
foreach (var a in result1.Entities)
{
Console.WriteLine("Name: " + a.Attributes["firstname"] + " " + a.Attributes["lastname"]);
}
Console.WriteLine("---------------------------------------");
See Also
QueryExpression
Build queries with QueryExpression
Use the ColumnSet class
Use the ConditionExpression class
Use the FilterExpression class
Microsoft Dynamics 365
© 2016 Microsoft. All rights reserved. Copyright