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 :
Microsoft Dynamics 365 | Integration, Dataverse...
Suggested Answer

operator '==' cannot be applied to operands of type 'optionsetvalue' and 'int'

(0) ShareShare
ReportReport
Posted on by 515


When working on a code upgrade I'm having troube comparing a optionsetvalue to an int.

The fix I put in (via a visual studio suggestion) doesn't work.

Can anyone point me in the right direction?

Thanks

my code is

IEnumerable<int?> myVariable = from entity in serviceContext.crm_entitySet
where entity.statuscode == 1
&& entity.crm_enityItemId.Id == _entityItemId
select entity.statuscode;

the error is on there "where entity.statuscode == 1" line

I have the same question (0)
  • Suggested answer
    meelamri Profile Picture
    13,216 User Group Leader on at

    Hi,

    try this: "where entity.statuscode.Value == 1"

  • Lee-Martin Profile Picture
    515 on at

    Thanks for replying, however it was the first thing I try and brings up the new error

    Cannot implicidly convert type 'System.Linq.IQueryable<Microsoft.Crm.Sdk.OptionSetValue>' to 'System.Collections.Generic.IEnumberable<int?>'

    and then suggests an explict cast for the whole piece of code not just that line

    However that cast was the fix I put in which has failed.

    Thanks

    Lee

  • Suggested answer
    meelamri Profile Picture
    13,216 User Group Leader on at

    Sorry for my partial answer.

    If you want to select the option set value only:

    IEnumerable<int?> myVariable = from entity in serviceContext.crm_entitySet

    where entity.statuscode.Value == 1

    && entity.crm_enityItemId.Id == _entityItemId

    select entity.statuscode.Value;

    If you want to select the OptionSetValue:

    IEnumerable<OptionSetValue?> myVariable = from entity in serviceContext.crm_entitySet

    where entity.statuscode.Value == 1

    && entity.crm_enityItemId.Id == _entityItemId

    select entity.statuscode;

    Good luck !

  • Lee-Martin Profile Picture
    515 on at

    Thanks, that's broadly solved the first problem.

    However I now need to convert that to an int. Using a new line of code.

    Everything I'm trying to convert that is failing, do you have any ideas?

    Thanks

    Lee

  • Lee-Martin Profile Picture
    515 on at

    I've tried casting the result into what I need, but I'm now getting this error

    Unable to cast object of type 'Microsoft.Xrm.Sdk.Linq.Query`1[Microsoft.Xrm.Sdk.OptionSetValue]' to type 'System.Collections.Generic.IEnumerable`1[System.Nullable`1[System.Int32]]'

  • Lee-Martin Profile Picture
    515 on at

    Converting the linq statement to OptionSetValue, then adding a ToList() to the result before the Count() was the solution to this.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

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

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 76

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans