Wednesday, May 25, 2016

Multiple conditions for a FilterExpression in CRM 2015

(A_logicalName AND B_LogicalName) OR (B_LogicalName AND C_LogicalName)





FilterExpression filter = new FilterExpression(LogicalOperator.Or);

FilterExpression filter1 = new FilterExpression(LogicalOperator.And);
filter1.Conditions.Add(new ConditionExpression("A_LogicalName", ConditionOperator.Equal, id1));
filter1.Conditions.Add(new ConditionExpression("B_LogicalName", ConditionOperator.Equal, id2));

FilterExpression filter2 = new FilterExpression(LogicalOperator.And);
filter2.Conditions.Add(new ConditionExpression("B_LogicalName", ConditionOperator.Equal, id2));
filter2.Conditions.Add(new ConditionExpression("C_LogicalName", ConditionOperator.Equal, id3));

filter.AddFilter(filter1);
filter.AddFilter(filter2);

query.Criteria = filter;





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)
      }
    }
  }
}
...




Reference -

http://stackoverflow.com/questions/14318160/multiple-conditions-for-a-filterexpression
https://msdn.microsoft.com/en-us/library/microsoft.xrm.sdk.query.queryexpression.aspx


No comments:

Post a Comment