How to create ConditionExpression for QueryExpression on statecode in early bound?

Konrad Viltersten picture Konrad Viltersten · Aug 17, 2013 · Viewed 8.7k times · Source

In late bound I'd use something like this to add a condition for only getting active records.

new ConditionExpression
{
  AttributeName = "statecode",
  Operator = ConditionOperator.NotEqual,
  Values = { SomeClass.Active }
}

But how do I express that in late bound?

Also, why does MS demand to cast it to String instead of int?

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.

Answer

MarioZG picture MarioZG · Aug 17, 2013

Something like

ConditionExpression condition1 = new Microsoft.Xrm.Sdk.Query.ConditionExpression
{
    AttributeName = "statecode",
    Operator = ConditionOperator.Equal,
    Values = { "Active" } //or SomeEnum.Active.ToString() if you want use good practice
};

should work fine.

Edit: other option is to cast enums to int: Values = {(int)SomeEnum.Active}