Build queries with QueryExpression


Applies To: Dynamics 365 (online), Dynamics 365 (on-premises), Dynamics CRM 2016, Dynamics CRM Online

In Microsoft Dynamics 365 (online & on-premises), you can use the QueryExpression class to programmatically build a query containing data filters and search conditions that define the scope of a database search. A query expression is used for single-object searches. For example, you can create a search to return all accounts that match certain search criteria. The QueryBase class is the base class for query expressions. There are two derived classes: QueryExpression and QueryByAttribute. The QueryExpression class supports complex queries. The QueryByAttribute class is a simple means to search for entities where attributes match specified values.

Query expressions are used in methods that retrieve more than one record, such as the IOrganizationService.RetrieveMultiple method, in messages that perform an operation on a result set specified by a query expression, such as BulkDeleteRequest and when the ID for a specific record is not known.

In addition, there is a new attribute on the organization entity, Organization.QuickFindRecordLimitEnabled. When this Boolean attribute is true, a limit is imposed on quick find queries. If a user provides search criteria in quick find that is not selective enough, the system detects this and stops the search. This supports a faster form of quick find and can make a big performance difference.


Don’t retrieve all attributes in a query because of the negative effect on performance. This is particularly true if the query is used as a parameter to an update request. In an update, if all attributes are included this sets all field values, even if they are unchanged, and often triggers cascaded updates to child records.

There are two additional ways to create queries to retrieve records from Microsoft Dynamics 365. FetchXML, the proprietary Microsoft Dynamics 365 query language, can be used to perform some queries by using XML-based queries. For more information, see Build queries with FetchXML. You can also use .NET Language-Integrated Query (LINQ) to write queries. More information:  Build queries with LINQ (.NET language-integrated query).

To save a query, you can convert it to FetchXML by using the QueryExpressionToFetchXmlRequest and save it as a saved view by using the userquery entity.

In This Section

Use the QueryByAttribute class

Use the QueryExpression class

Use the ColumnSet class

Use the ConditionExpression class

Use the FilterExpression class

Use a left outer join in QueryExpression to query for records "not in"

Test for a null value

Page large result sets with QueryExpression

Sample: Retrieve with one-to-many relationship

Sample: Retrieve multiple with the QueryByAttribute class

Sample: Retrieve multiple with the QueryExpression class

Sample: Use QueryExpression with a paging cookie










See Also

Sample: Convert queries between Fetch and QueryExpression

Microsoft Dynamics 365

© 2016 Microsoft. All rights reserved. Copyright