Поделиться через


Use the ConditionExpression class

 

Applies To: Dynamics CRM 2015

In Microsoft Dynamics CRM 2015 and Microsoft Dynamics CRM Online 2015 Update, you can use the ConditionExpression class to compare an attribute 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 attribute in the condition expression.

Operator

Specifies the condition operator. This is set by using the ConditionOperator enumeration.

Values

Specifies the values of the attribute.

When using the AddCondition 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 an attribute value to an enumeration, such as a state code, you must use the ToString method to convert the value to a string.

Example

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

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

See Also

ConditionExpression
Build queries with QueryExpression
Build queries with QueryExpression
Use the FilterExpression class

© 2016 Microsoft. All rights reserved. Copyright