How TO :Get Text Value of an OptionSetValue Using LINQ in Microsoft Dynamics CRM 2011


i have been always trying to get the label of an option set value by the way described in the following Article  which uses RetrieveAttributeRequest and passed the value which we want to get the label for.

CRM SDK Provides another new way instead of the one described in the above link which is using LINQ To CRM Queries.

The basics are as follows. 

1. Use CrmSvcUtil.exe (comes with the SDK) to get a context that you can use with the CRM LINQ provider.

2. Use LINQ to pull the entity attribute FormattedValues for a specific attribute of type OptionSetValue.  The example below is pulling based on a specific entity ID so it pulls the specific text value of the optionset in this instance.

var strstringvalue = (from ca in context.CreateQuery<CampaignActivity>() where ca.ActivityId == campaignResponse.new_CampaignActivityId.Id select ca.FormattedValues["new_campaignactivitytype"]);