Hello, I am trying to retrieve the Value of an OptionSet Attribute from a collection of entities. I want all the values stored into a list for the following FetchXML query:
//Initialize serving Group list
var servingList = new List<int>();
string availableGroups = @" <fetch distinct='true' >
<entity name='contract' >
<link-entity name='contractdetail' from = 'contractid' to = 'contractid' alias='line' >
<attribute name='new_servinggroup' />
<filter type='and' >
<condition value='0' attribute='statecode' operator= 'eq' />
<condition value='" + targetId + @"' attribute='contractid' operator= 'eq' />
</filter >
</link-entity>
</entity >
</fetch>";
EntityCollection availableGroups_result =
service.RetrieveMultiple(new FetchExpression(availableGroups));
foreach (var serving in availableGroups_result.Entities)
{
var aliased = (AliasedValue)serving.Attributes["line.new_servinggroup"];
var value = (int)aliased.Value; //InvalidCastException HERE!
servingList.Add(value);
}
Can someone explain to me why I am getting an InvalidCastException and how to fix it?! I want the actual numeric value of each OptionSet returned from each entity. I then want to put these values into the ServingList... Thanks in advance for any help!