12

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

带有过滤器的条件表达式:

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

// 过滤器2

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

3 回答 3

24

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

(A_logicalName AND B_LogicalName) 或 (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上的示例。

于 2013-01-14T12:59:26.693 回答
13

这是我正在使用的。出于某种原因,我发现它更容易掌握。这可能是因为我使用初始化语法所以 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)
      }
    }
  }
}
...
于 2013-01-15T07:54:11.710 回答
11

健康)状况:

(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上找到更复杂的查询。

于 2013-12-27T16:47:27.113 回答