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 CRM (Archived)

How to Add an optionset filter criteria in MS CRM Query Expression?

(1) ShareShare
ReportReport
Posted on by

I have an entity LeaveType with two attributes, 1. Type, 2. Available Days, where Type is an optionset and Available days is a text field. I want to fetch all such LeaveType records where the Type = 'Annual' selected in the optionset. I am not able to find how to add a filter the query expression for the option set value. Below is my in progress method:

public Entity Getleavetype(Guid LeaveDetailsId, IOrganizationService _orgService, CodeActivityContext Acontext)
        {
            QueryExpression GetLeavedetails = new QueryExpression();
            GetLeavedetails.EntityName = "sgfdhr_leavetype";
            GetLeavedetails.ColumnSet = new ColumnSet("new_type");
            GetLeavedetails.ColumnSet = new ColumnSet("new_availabledays");
            GetLeavedetails.Criteria.AddCondition("new_type", ConditionOperator.Equal,   "Annual" ); //Is this correct????
            GetLeavedetails.Criteria.AddCondition("new_employeeleavecalculation", ConditionOperator.Equal, LeaveDetailsId); //ignore this

            //((OptionSetValue)LeaveDetailsId["new_leavetype"]).Value

            EntityCollection LeaveDetails = _orgService.RetrieveMultiple(GetLeavedetails);
            return LeaveDetails[0];
        }


Please advise.

Thanks, 

Fahad Ali Shaikh

*This post is locked for comments

I have the same question (0)
  • Guido Preite Profile Picture
    54,086 Moderator on at

    In your condition you need to set the integer value of the optionset, not the label.

    Assuming that Annual value is for example 2, the code will be:

    GetLeavedetails.Criteria.AddCondition("new_type", ConditionOperator.Equal, 2);

  • Kevin T 9 Profile Picture
    105 on at

    How do you check which integer value corresponds with a given text value?  Is it assumed you'll figure it out and hard code the value, or is there a way to look up the option set key/value pairs?

  • Community Member Profile Picture
    on at

    Hi kevin,

    You can check the optionset Value and Text  in the field  properties.

    1_2D00_Division_2D00_Option_2D00_Set.png

    Hope it helps :)

    Thanks ,

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!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
ScottDurow Profile Picture

ScottDurow 2

#2
GJones Profile Picture

GJones 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans