CRM 2011: retrieve OptionSetValue Name in Linq

With CRM 2011, when querying data using Linq, you might find it hard to retrieve an OptionSetValue name or label. You might end up thinking about making a call to the metadata service. Don’t. Just be aware of two things:

1. The syntax is as follows:

from a in AccountSet
select new {
myvalue = a.FormattedValues["crm_myoptionset"]
}

2. But, if it’s null, this code will break, as null is sent instead of an OptionSetValue object.

So, I use this:

from a in AccountSet
select new {
myvalue = (a.crm_myoptionset != null) ?  a.FormattedValues["crm_myoptionset"] : “”
}

Leave a Reply