Use the ConditionExpression class


Unsure about entity vs. table? See Developers: Understand terminology in Microsoft Dataverse.

In Microsoft Dataverse, you can use the ConditionExpression class to compare a table column to a value or set of values by using an operator, such as “equal to” or “greater than”. The ConditionExpression class lets you pass condition expressions as parameters to other classes, such as QueryExpression and FilterExpression.

The following table lists the properties you can set to create a condition using the ConditionExpression class.

Property Description
AttributeName Specifies the logical name of the column in the condition expression.
Operator Specifies the condition operator. This is set by using the ConditionOperator enumeration.
Values Specifies the values of the column.

When using the AddCondition(ConditionExpression) method (or the constructor for ConditionExpression), it’s important to understand whether the array is being added as multiple values or as an array.

The following code example shows two different outcomes depending on how the array is used.

string[] values = new string[] { "Value1", "Value2" };  
ConditionExpression c = new ConditionExpression("name", ConditionOperator.In, values);  
Console.WriteLine(c.Values.Count); //This will output 2   
string[] values = new string[] { "Value1", "Value2" }object value = values;  
ConditionExpression c = new ConditionExpression("name", ConditionOperator.In, value);  
Console.WriteLine(c.Values.Count); //This will output 1  

In some cases, it is necessary to cast to either object[] or object, depending on the desired behavior.

When you create a condition that compares a column value to an enumeration, such as a state code, you must use the ToString method to convert the value to a string.

Example: use the ConditionExpression class

The following code example shows how to use the ConditionExpression class.

//  Query using ConditionExpression    
ConditionExpression condition1 = new ConditionExpression();  
condition1.AttributeName = "lastname";    
condition1.Operator = ConditionOperator.Equal;    
FilterExpression filter1 = new FilterExpression();    
QueryExpression query = new QueryExpression("contact");    
query.ColumnSet.AddColumns("firstname", "lastname");    
EntityCollection result1 = _serviceProxy.RetrieveMultiple(query);    
Console.WriteLine("Query using Query Expression with ConditionExpression and FilterExpression");    
foreach (var a in result1.Entities)    
      Console.WriteLine("Name: " + a.Attributes["firstname"] + " " + a.Attributes["lastname"]);    

Example: test for inactive state

The following code example shows how to use the ConditionExpression class to test for the inactive state.

ConditionExpression condition3 = new ConditionExpression();  
condition3.AttributeName = "statecode";  
condition3.Operator = ConditionOperator.Equal;  

Column comparison using the SDK API

The following example shows how to compare columns using SDK API and the Organization service:

public ConditionExpression
  string attributeName,
  ConditionOperator conditionOperator,
  bool compareColumns,
  object value

public ConditionExpression
  string attributeName,
  ConditionOperator conditionOperator,
  bool compareColumns,
  object[] values

By passing in true as the value for the compareColumns parameter, the value is treated as the name of the second column to compare the values in attributeName to. Pass in false to treat it as a literal value instead.

For example:

new ConditionExpression("firstname", ConditionOperator.Equal, true, "lastname");

This code creates a condition to return only records where the first and last names are the same, while

new ConditionExpression("firstname", ConditionOperator.Equal, false, "John");

creates a condition to return only records where the first name is John.

More information: Use column comparison in queries

Use Wildcard characters in conditions using string values

You can use wildcard characters when you construct queries using conditions on string values. More information: Use wildcard characters in conditions for string values

See also

Building Queries
Build Queries with QueryExpression
Use the FilterExpression Class