Skip to main content

Notifications

Microsoft Dynamics 365 | Integration, Dataverse...
Suggested answer

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

Posted on by 509


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

  • Lee-Martin Profile Picture
    Lee-Martin 509 on at
    RE: operator '==' cannot be applied to operands of type 'optionsetvalue' and 'int'

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

  • Lee-Martin Profile Picture
    Lee-Martin 509 on at
    RE: operator '==' cannot be applied to operands of type 'optionsetvalue' and 'int'

    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
    Lee-Martin 509 on at
    RE: operator '==' cannot be applied to operands of type 'optionsetvalue' and 'int'

    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

  • Suggested answer
    meelamri Profile Picture
    meelamri 13,204 User Group Leader on at
    RE: operator '==' cannot be applied to operands of type 'optionsetvalue' and 'int'

    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
    Lee-Martin 509 on at
    RE: operator '==' cannot be applied to operands of type 'optionsetvalue' and 'int'

    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
    meelamri 13,204 User Group Leader on at
    RE: operator '==' cannot be applied to operands of type 'optionsetvalue' and 'int'

    Hi,

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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans