【问题标题】:How to use multiple Filters while executing K2 Smart Object from API?从 API 执行 K2 智能对象时如何使用多个过滤器?
【发布时间】:2013-04-01 08:49:50
【问题描述】:

我正在尝试执行自定义 K2 智能对象的 Get List 方法,该方法查询 SQL 数据库对象(视图)。我在现有的 C# 项目中使用 API 来执行这些智能对象。我想将多个过滤器传递给这个 GetList 方法。

我正在使用下面的代码添加单个过滤器,但无法找到传递多个过滤器的方法:

Dictionary<string, string>[] results = null;
if (!server.Connection.IsConnected) return results;

SmartObject smartObject = server.GetSmartObject(objectName);
SmartListMethod newList = smartObject.ListMethods["GetList"];
smartObject.MethodToExecute = methodName;

Contains myFilter = new Contains();
myFilter.Left = new PropertyExpression(filterPropertyName, PropertyType.Text);
myFilter.Right = new ValueExpression(filterValue, PropertyType.Text);

newList.Filter = myFilter;

SmartObjectList list = server.ExecuteList(smartObject);

results = new Dictionary<string, string>[list.SmartObjectsList.Count];
for (int i = 0; i < list.SmartObjectsList.Count; i++)
{
    results[i] = new Dictionary<string, string>();
    SmartObject smo = list.SmartObjectsList[i];
    foreach (SmartProperty property in smo.Properties)
    {
         results[i].Add(property.Name, property.Value);
    }
}
return results;

就像上面代码中的myFilter,我想传递多个这样的过滤器。

【问题讨论】:

  • 找到解决方案,需要使用AND/OR逻辑过滤器来组合其他过滤器,如包含和等于。
  • 只是想添加一个注释,其中包含如何为他人执行此操作的代码(以及我自己在睡了几晚之后又忘记了)。感谢您的回答!

标签: k2


【解决方案1】:
Dictionary<string, string>[] results = null;
if (!server.Connection.IsConnected) return results;

SmartObject smartObject = server.GetSmartObject(objectName);
SmartListMethod newList = smartObject.ListMethods["GetList"];
smartObject.MethodToExecute = methodName;

Contains myFilter = new Contains();
myFilter.Left = new PropertyExpression(filterPropertyName, PropertyType.Text);
myFilter.Right = new ValueExpression(filterValue, PropertyType.Text);

    Contains myFilter2 = new Contains();
    myFilter2.Left = new PropertyExpression(filterPropertyName2, PropertyType.Text);
    myFilter2.Right = new ValueExpression(filterValue2, PropertyType.Text);

    Not myFilter3 = new Not(myFilter2);

    // And can have the left and right filters set to pre-defined filteres to combined them!
    And myCombinedFilter = new And();
    myCombinedFilter.Left = myFilter;
    myCombinedFilter.Right = myFilter2;

    // Or also works!
    Or myCombinedFilterOr = new Or();
    myCombinedFilterOr.Left = myCombinedFilter;
    myCombinedFilterOr.Right = myFilter3;

// ((MyFilter && myFilter2) || Not MyFilter2)
newList.Filter = myCombinedFilterOr;

SmartObjectList list = server.ExecuteList(smartObject);

results = new Dictionary<string, string>[list.SmartObjectsList.Count];
for (int i = 0; i < list.SmartObjectsList.Count; i++)
{
    results[i] = new Dictionary<string, string>();
    SmartObject smo = list.SmartObjectsList[i];
    foreach (SmartProperty property in smo.Properties)
    {
         results[i].Add(property.Name, property.Value);
    }
}
return results;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 2021-04-29
    • 2017-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多