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

Query Expression Add Criteria for look up field and optionset.

(0) ShareShare
ReportReport
Posted on by

Hey guys, i m trying to get data from field with one criteria.
For example: 

QueryExpression query = new QueryExpression { EntityName = "Account", ColumnSet = new ColumnSet("accounttype") };
                    query.Criteria.AddCondition("location", ConditionOperator.Equal, "India");


The field of location is optionset. I tried to run this code, but its keep giving me error message. When i tried to change the condition to a field with single line of text type. Its work  !!

im wondering that my code can't get value from field except single line of text, multiple text and number. 
So my question is, how can i add criteria from a optionset type of field and lookup type of field

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Clem Profile Picture
    2,541 on at

    Hi Saraswati,

    For filtering on OptionSet you need to put the interger value instead of the Label.

    So if you go to your entity and then the field you are filtering on "location" you should have the list of values and for India you should see the Value of it.

    For the Lookup field, you just need to use the Id of the field, let's assume that your lookup field is Location, you need to add the following where "locationIdYouWant" id the Guid of the India entry in your entity Location.

    query.Criteria.AddCondition("location", ConditionOperator.Equal, locationGuidYouWant);

    Hope it helps,

    Clément

  • Community Member Profile Picture
    on at

    hi clem, i was used the integer value for option set. but it stil give me error message.

    query.Criteria.AddCondition("location", ConditionOperator.Equal, 100,000,002);

    is that the correct ways to write the  code ?

  • Verified answer
    Hemant Kumar Sahu Profile Picture
    1,829 on at

    Hi Saraswati,

    Instead of comparing with string "India" please compare with value.

    query.Criteria.AddCondition("location", ConditionOperator.Equal, optionsetvalueofIndia);
    Example:
    query.Criteria.AddCondition("location", ConditionOperator.Equal, 100000002);

    or
    query.Criteria.AddCondition("location", ConditionOperator.Equal, "100000002");

    Thanks

    Hemant

    If my suggestion helped you please mark this post as suggested answer.

  • Community Member Profile Picture
    on at

    hey thanks for the answer, thats work for me.

    But how  can i did that if using look up form ??

  • Verified answer
    Hemant Kumar Sahu Profile Picture
    1,829 on at

    Hi Saraswati,

    I guess you want to compare lookup value.

    Suppose 'Location' is lookup field.

    query.Criteria.AddCondition("location", ConditionOperator.Equal, new Guid("guidofrecord"););

    Thanks

    Hemant

  • Community Member Profile Picture
    on at

    Thanks hemant

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

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans