Skip to main content

Notifications

Announcements

No record found.

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!

Posted on by Microsoft Employee

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:
  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: InvalidCastException When retrieving AliasValue of an OptionSet and Casting it to an integer!

    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);

       }

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

    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

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

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans