web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Forums / Field Service forum / InvalidCastException W...
Field Service forum

InvalidCastException When retrieving AliasValue of an OptionSet and Casting it to an integer!

(0) ShareShare
ReportReport
Posted on by

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!

Categories:
I have the same question (0)
  • lokesh@dynamisity Profile Picture
    95 on at

    Hi Alan

    you are getting InvalidCastException because you are trying to convert OptionSetValue to Int

    Try to use below code

    foreach (var serving in availableGroups_result.Entities)

               {

                   var aliased = (AliasedValue)serving.Attributes["line.new_servinggroup"];

                   var value = ((OptionSetValue)aliased.Value).Value;  //Use this code

                   servingList.Add(value);

               }

    I hope this will help you

    Please verify answer if it works

    Thanks  

    Lokesh

  • Verified answer
    Community Member Profile Picture
    on at

    You are right, but your suggested answer is wrong!

    This is the correct casting:

    Aliased Value -> Object -> OptionSetValue -> int

    This is what I had to do:

    foreach (var serving in availableGroups_result.Entities)

       {

           var value = (int)((OptionSetValue)(serving.GetAttributeValue<AliasedValue>("line.new_servinggroup").Value).Value);

       }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Field Service

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans