Multiple conditions for a FilterExpression
Asked Answered
I

3

15

I am using Condition expression but I'm unable to add more than one condition to a FilterExpression. Can any one help? I have posted my source code here.

ConditionExpression with Filters:

  // Filter1
FilterExpression filter1 = new FilterExpression();
filter1.FilterOperator = LogicalOperator.And;
filter1.Conditions.Add(new ConditionExpression("A_LogicalName", ConditionOperator.Equal, id1));
filter1.Conditions.Add(new ConditionExpression("B_LogicalName", ConditionOperator.Equal, id2));
query.Criteria.Filters.Add(filter1);

// Filter2

FilterExpression filter2 = new FilterExpression();
filter2.FilterOperator = LogicalOperator.And;
filter2.Conditions.Add(new ConditionExpression("B_LogicalName", ConditionOperator.Equal, id3));
filter2.Conditions.Add(new ConditionExpression("C_LogicalName", ConditionOperator.Equal, id4));
q_ShoppingCartItemQuantityCheck.Criteria.Filters.Add(filter2);  
Incompressible answered 14/1, 2013 at 12:9 Comment(1)
When it comes to more complex expressions, I found out that I have hard time setting them up the way you did. It's doable but not as transparent as the initializer approach. When I get to work in an hour, I'll post an alternative example for you. Hopefully it'll be more expandable for you. (Also, you need to accept one of the answers - mine will be of course the best one, haha.)Erlineerlinna
L
25

Not sure if I understand your question correctly. If I got it right, you want to build a hierarchical expression like this:

(A_logicalName AND B_LogicalName) OR (B_LogicalName AND C_LogicalName)

is that correct?

Wouldn't the following then solve your problem?

query.Criteria = new FilterExpression(); 
query.Criteria.FilterOperator = LogicalOperator.Or;

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

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

See also example on MSDN.

Liselisetta answered 14/1, 2013 at 12:59 Comment(1)
any Fluent helper version about it?Misadventure
E
15

Here's what I'm using. For some reason, I find it clearer to grasp. It's probably due to the fact that I use initializer syntax so YMMV.

...
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)
      }
    }
  }
}
...
Erlineerlinna answered 15/1, 2013 at 7:54 Comment(1)
I find that much better than anything else I have seenInfective
N
11

Condition:

(A_logicalName AND B_LogicalName) OR (B_LogicalName AND C_LogicalName)

can be expressed in this way too:

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;

A more complex query can be found on MSDN.

Ngocnguyen answered 27/12, 2013 at 16:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.