Build queries with QueryExpression:
1. 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.
2. The QueryBase class is the base class for query expressions. There are three derived classes: QueryExpression, QueryByAttribute, and FetchExpression.
- The
QueryExpression
the class supports complex queries. - The
QueryByAttribute
class is a simple means to search for entities where attributes match specified values. - The third derived class,
FetchExpression
is used with FetchXML, the proprietary Dataverse query language, can be used to perform some queries by using XML-based queries.
Use the QueryExpression class
// Query using ConditionExpression and FilterExpressionConditionExpression condition1 = new ConditionExpression();
condition1.AttributeName = "lastname";
condition1.Operator = ConditionOperator.Equal;
condition1.Values.Add("White");
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("---------------------------------------");
Use the FilterExpression class
In Microsoft Dataverse, you can use the FilterExpression class to build a query that expresses multiple conditions. For example, you can create a query expression that is the equivalent of a SQL statement such as ([FirstName] = 'Mike' OR [FirstName] = 'Zen') AND [City] = 'Pune'
.
The below example shows how to use the FilterExpression class.
query.ColumnSet.AddColumns("firstname", "lastname", "address1_city");
query.Criteria = new FilterExpression();
query.Criteria.AddCondition("address1_city", ConditionOperator.Equal, "Pune");
FilterExpression childFilter = query.Criteria.AddFilter(LogicalOperator.Or);
childFilter.AddCondition("lastname", ConditionOperator.Equal, "John");
childFilter.AddCondition("lastname", ConditionOperator.Equal, "White");
// Pass query to service proxy
EntityCollection results = _serviceProxy.RetrieveMultiple(query);
Console.WriteLine();
Console.WriteLine("Query using QE with multiple conditions and filters");
Console.WriteLine("---------------------------------------");
// Print results
foreach(var a in results.Entities)
{
Console.WriteLine("Name: {0} {1}", a.GetAttributeValue<string>("firstname"), a.GetAttributeValue<string>("lastname"));
Console.WriteLine("City: {0}", a.GetAttributeValue<string>("address1_city"));
}
Console.WriteLine("---------------------------------------");
References: MS Docs
Thanks!!!
Happy Learning ...
No comments:
Post a Comment