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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to put And and Or Operator together in Conditions in QueryExpressions

(1) ShareShare
ReportReport
Posted on by

How to apply following type of condtion in Query Expression

(Condition 1 and Condition 2) or (Condition 3 and Condition 4)

var queryExpression = new QueryExpression()

                   {

                       Distinct = false,

                       EntityName = Incident.EntityLogicalName,

                       ColumnSet = new ColumnSet("incidentid","productid", "title", "ticketnumber", "statuscode", "severitycode", "prioritycode", "createdon", "modifiedon", "createdby"),

Send

                       Criteria =

                       {

                           Conditions =

                               {

                                   Condition 1,

                                   Condition 2,

                                   Condition 3,

                                   Condition 4

                               }

                       }

                   };

in Above query expr all conditions are with and operator by default, I want them to work like above conditions written with or & and combined.

*This post is locked for comments

  • Community Member Profile Picture
    on at
    RE: How to put And and Or Operator together in Conditions in QueryExpressions

    msdn.microsoft.com/.../gg309410.aspx

    hope this post help you

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: How to put And and Or Operator together in Conditions in QueryExpressions

    I think you need something like this:

    Criteria = new FilterExpression
    {
      FilterOperator = LogicalOperator.Or,
      Filters =
      {
        new FilterExpression
        {
          FilterOperator = LogicalOperator.And,
          Conditions =
          {
            new ConditionExpression("field1", ConditionOperator.NotNull),
            new ConditionExpression("field2", ConditionOperator.NotNull)
          }
        },
        new FilterExpression
        {
          FilterOperator = LogicalOperator.And,
          Conditions =
          {
            new ConditionExpression("field3", ConditionOperator.NotNull),
            new ConditionExpression("field4", ConditionOperator.NotNull)
          }
        }
      }
    }
  • Suggested answer
    Shekhar Shinde Profile Picture
    on at
    RE: How to put And and Or Operator together in Conditions in QueryExpressions

    Hello Neeraj ,

    use  below code for your requirement.

                QueryExpression queryEntity = new QueryExpression("logicalNameOfTheEntity");
                queryEntity.ColumnSet = new ColumnSet("attribute1", "attribute2");
                
                FilterExpression Filter1 = new FilterExpression(LogicalOperator.And);
                Filter1.AddCondition("field1_logicalname", ConditionOperator.Equal, value1);
                Filter1.AddCondition("field2_logicalname", ConditionOperator.Equal, value2);
    
                FilterExpression Filter2 = new FilterExpression(LogicalOperator.And);
                Filter2.AddCondition("field3_logicalname", ConditionOperator.Equal, value3);
                Filter2.AddCondition("field4_logicalname", ConditionOperator.Equal, value4);
    
                FilterExpression mainFilter = new FilterExpression(LogicalOperator.Or);
                mainFilter.AddFilter(Filter1);
                mainFilter.AddFilter(Filter2);
                queryEntity.Criteria = mainFilter;
                EntityCollection queryEntityRecords = service.RetrieveMultiple(queryEntity);
                foreach (var entityRecord in queryEntityRecords.Entities)
                {
                    
                    //Your code logic here
                }
  • Suggested answer
    Arpit Shrivastava Profile Picture
    on at
    RE: How to put And and Or Operator together in Conditions in QueryExpressions

    Hi Neeraj,

    If you have to make the complex query or need to put a lot of conditions, better to go with Fetch XML. It would be easy to manage with the minimal line of code.

    You can easily Construct it from Advanced Find and use it like below:

    // Retrieve all Contacts having (firstname contains 'Arpit' AND lastname contains 'Shrivastava') OR (email contains 'arpit.crmconsultant@gmail.com')

    string query = @"

       <fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>

      <entity name='contact'>

        <attribute name='fullname' />

        <attribute name='firstname' />

        <attribute name='lastname' />

        <attribute name='emailaddress1'/>

        <attribute name='telephone1' />

        <attribute name='contactid' />

        <order attribute='fullname' descending='false' />

        <filter type='and'>

          <filter type='or'>

            <filter type='and'>

              <condition attribute='firstname' operator='like' value='%Arpit%' />

              <condition attribute='lastname' operator='like' value='%Shrivastava%' />

            </filter>

            <condition attribute='emailaddress1' operator='like' value='%arpit.crmconsultant@gmail.com%' />

          </filter>

        </filter>

      </entity>

    </fetch>"; 

    EntityCollection result = _serviceProxy.RetrieveMultiple(new FetchExpression(query));

    foreach (var data in result.Entities)   

    {   

    System.Console.WriteLine(data.Attributes['firstname']);   

    }

    Can check for more info:

    https://arunpotti.wordpress.com/2014/12/10/retrieve-records-using-fetchxml-c-sdk-in-crm/

    https://msdn.microsoft.com/en-in/library/gg328117.aspx?f=255&mspperror=-2147217396#Anchor_1

    If you find the answer useful, please mark it as verified

    Cheers

    Arpit

    https://arpitmscrmhunt.blogspot.in

     

  • Suggested answer
    Community Member Profile Picture
    on at
    RE: How to put And and Or Operator together in Conditions in QueryExpressions

    For: (Condition 1 and Condition 2) or (Condition 3 and Condition 4)

    I use something like this:

    // First AND-clause

    var filterA = new FilterExpression(LogicalOperator.And);

    filterA.Conditions.Add(Condition1);

    filterA.Condition.Add(Condition2);

    // Second AND-clause

    var filterB = new FilterExpression(LogicalOperator.And);

    filterB.Conditions.Add(Condition3);

    filterB.Conditions.Add(Condition4);

    // Main OR-clause

    var filterC = new FilterExpression(LogicalOperator.Or); // The OR-clause

    filterC.AddFilter(filterA);

    filterC.AddFilter(filterB);

    // Create the QueryExpression.

    var myQuery = new QueryExpression

    {

    EntityName = Incident.EntityLogicalName,

    Distinct = false,

    ColumnSet = new ColumnSet("incidentid","productid", "title", "ticketnumber", "statuscode", "severitycode", "prioritycode", "createdon", "modifiedon", "createdby"),

    Criteria = filterC

    };

  • Suggested answer
    Pawar Pravin  Profile Picture
    on at
    RE: How to put And and Or Operator together in Conditions in QueryExpressions

    Hi ,

    As suggested by all you need to use filter expression to sort out query by AND & OR.
    Please do refer following snapshot for your reference:

    7635.Capture.JPG

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…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans