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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Unable to cast object of type 'Microsoft.Xrm.Sdk.OptionSetValue' to type 'System.String'

(0) ShareShare
ReportReport
Posted on by 35

Hi, i am trying to read the value of an OptionSetValue using the code below, but it is not working and it's throwing the error :

Error of Plugin: System.InvalidCastException: Unable to cast object of type 'Microsoft.Xrm.Sdk.OptionSetValue' to type 'System.String'.
at Microsoft.Xrm.Sdk.Entity.GetAttributeValue[T(String attributeLogicalName).

Any ideas?

                    if (entity.Attributes.Contains("addresstypecode"))
                    {
                        var address_type = entity.GetAttributeValue("addresstypecode").Value.ToString();
                        QueryExpression query = new QueryExpression("customeraddress");
                        query.ColumnSet = new ColumnSet("addresstypecode");
                        query.Criteria.AddCondition("addresstypecode", ConditionOperator.Equal, address_type);
                        EntityCollection collection = service.RetrieveMultiple(query);
 

I have the same question (0)
  • Verified answer
    David Jennaway Profile Picture
    14,065 on at

    You shouldn't cast the OptionSetValue to a string. If you change line 3 to the following it should work

    var address_type = entity.GetAttributeValue<OptionSetValue>("addresstypecode").Value;

  • Verified answer
    Pankaj Gogoi Profile Picture
    3,177 on at

    Hi Jamal,

    Can you try with this.

    var address_type = ((OptionSetValue)entity["addresstypecode"]).Value;

    Best Regards

    PG

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Pallavi Phade Profile Picture

Pallavi Phade 102 Super User 2026 Season 1

#2
Abhilash Warrier Profile Picture

Abhilash Warrier 55 Super User 2026 Season 1

#3
ManoVerse Profile Picture

ManoVerse 47 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans