Use the ConditionExpression class

Note

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;    
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("---------------------------------------");  

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;  
condition3.Values.Add(AccountState.Active);  
  

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

See also

Building Queries
Build Queries with QueryExpression
Use the FilterExpression Class
ConditionExpression