3

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.

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438

1 Answers1

6

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}

MarioZG
  • 2,087
  • 11
  • 19
  • I don't get why. If we use the string "Active", we shouldn't get the same behavior as if we use an enum (which is an *int*). Also, your use of the enum makes me suspect that you're talking early bound. Is it so? – Konrad Viltersten Aug 17 '13 at 20:17
  • Me neither :) Must be some issue when casting enums in SDK code. I have mentioned enum, just to point out that you can create your own enum to avoid hardcoding strings or ints. – MarioZG Aug 17 '13 at 21:07
  • 2
    conditions on statecode and statuscode accept both int and string, don't ask me why but works – Guido Preite Aug 17 '13 at 22:47