Dynamics CRM 2015 – Querying Data with QueryExpression
Developers can utilize the power of the Microsoft Dynamics CRM 2011 SDK to build custom applications, plug-ins, and workflows which communicate with the CRM platform. Queries can be written to retrieve information from the CRM database in many different ways. In this blog, I will explain the use of the QueryExpression class and see how it can be used to write simple and complex queries.
QueryExpression is useful in scenarios where you want to return multiple entities that match a certain criteria. It lets you specify which fields you want to have returned (from the specified entity type or any related entity) as part of the query result in order to improve performance. Developers can also control the order in which records are returned.
To illustrate the power of QueryExpression, let’s take a look at a few examples:
Example 1 – Simple query with two conditions for a single entity
Scenario: Retrieve the First Name, Last Name, and Email Address for Contacts that have the Job Title field set and live in Auckland city
Example 2 – Complex query with multiple conditions across two related entities
Scenario: Retrieve the First Name, Last Name, and Email Address for Contacts that are “Sales Managers” and the Parent Customer is based in Auckland or Wellington city
Example 3 – Simple query with OrderExpression
Scenario: Retrieve all Accounts in the system and order by the date created (most recent first)
Those are just a few examples of how you can write queries using QueryExpression. Developers can use any combination of LinkEntity, ConditionExpression and OrderExpression to write complex queries.
Hi Abhishina...this is a very helpful post, but I'm wondering how much complexity can be built into a query expression...
ReplyDeletefor instance - how would I build a QueryExpression of several Logical "ANDs" joined by logical "ORs", as follows:
(A and B) OR (M and N) OR (X and Y)
I understand doing this"
queryExpression qe1 = new QueryExpression{entity, Columnset};
qe1.Criteria.AddCondition("field1",ConditionOperator.Equal, X);
qe1.Criteria.AddCondition("field2",ConditionOperator.Equal, Y);
qe1.Criteria.Filters.Add(new FilterExpression{FilterOperator = LogicalOperator.And});
...but what I don't understand is how to join a number of logical 'Ands' together with logical OR's?