FilterExpression的多个条件

时间:2013-01-14 12:09:02

标签: c# dynamics-crm-2011 query-expressions

我正在使用条件表达式,但我无法向FilterExpression添加多个条件。 任何人都可以帮忙吗?我在这里发布了我的源代码。

使用过滤器的ConditionExpression:

  // 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);  

3 个答案:

答案 0 :(得分:23)

不确定我是否正确理解了您的问题。 如果我做对了,你想建立一个像这样的分层表达式:

  

(A_logicalName AND B_LogicalName)OR(B_LogicalName AND   C_LogicalName)

是正确的吗?

以下不会解决您的问题吗?

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));

另见MSDN上的示例。

答案 1 :(得分:11)

这是我正在使用的。出于某种原因,我发现它更清楚。这可能是因为我使用初始化语法,所以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)
      }
    }
  }
}
...

答案 2 :(得分:10)

条件:

(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;

可以在MSDN上找到更复杂的查询。