locked
Howto compare OptionSetValues in C# RRS feed

  • Question

  • Hi,

    currently i have a early bound entity (strongly typed class) of the entity account. This entity field (anyFieldNameA) of type OptionSetValue (Values: ValueA,ValueB,ValueC). The field anyFieldNameA is using an already existing OptionSet (AnyOptionset). I created with CrmSvcUtil.exe also the strongly typed class of this already existing OptionSet.

    Now i want to compare those OptionSetValues. Currently i need to convert them to int and then it's working.

    if(account.anyFieldNameA.Value == (int)AnyOptionset.ValueA) {}

    Is there a nicer way to accomplish this?

    Kind Regards

    Markus

    Tuesday, April 29, 2014 11:02 AM

Answers

  • The OptionSetValue type is simply an integer assigned enum.  You can compare the osv.Value vs. an integer or an enum converted to int (as you did above).  You could also new up an OptionSetValue to use as a comparator:

    var osv = new OptionSetValue(int);

    • Marked as answer by Markus Benz Monday, May 5, 2014 8:23 AM
    Tuesday, April 29, 2014 12:36 PM

All replies

  • The OptionSetValue type is simply an integer assigned enum.  You can compare the osv.Value vs. an integer or an enum converted to int (as you did above).  You could also new up an OptionSetValue to use as a comparator:

    var osv = new OptionSetValue(int);

    • Marked as answer by Markus Benz Monday, May 5, 2014 8:23 AM
    Tuesday, April 29, 2014 12:36 PM
  • Thx JBlaeske
    Monday, May 5, 2014 8:23 AM