2

仅限 EPiServer:

如何搜索给定属性中具有任何值的页面?我可以搜索属性中具有特定值的页面,但我不知道如何搜索“非空”。

例如,这不起作用:

var criterias = newPropertyCriteriaCollection
{
  new PropertyCriteria()
  { 
    Condition = CompareCondition.NotEqual, 
    Name = "MyProperty", 
    IsNull = false, 
    Type = PropertyDataType.String, 
    Value = "" 
  }
};

var pages = DataFactory.Instance.FindPagesWithCriteria(PageReference.StartPage, criterias);

抛出异常,“crieria 值不能为 null 或为空。设置 IsNull 属性以搜索 null。”

有任何想法吗?

4

4 回答 4

2

是的,这很令人困惑。万一其他人偶然发现了这一点,以下是如何搜索将某个 PageReference 属性设置为某些内容的页面:

new PropertyCriteria()
{
    createdCriteria.Name = "MyProperty";
    createdCriteria.Type = PropertyDataType.PageReference;
    createdCriteria.Condition = EPiServer.Filters.CompareCondition.NotEqual;
    createdCriteria.Value = "0";
    createdCriteria.IsNull = false;
}
于 2014-04-10T12:43:42.120 回答
1

除非我错过了一个技巧,否则使用 E​​PiServer PropertyCriteriaCollection 似乎是不可能的。

我已经在反射器中进行了挖掘,这是我的发现。FPWC 方法最终调用 EPiServer.DataAccess.PropertySearchDB.FastFindPagesWithCriteria()。

在这个方法中有以下内容:

    foreach (PropertyCriteria criteria in criterias)
    {
      if (criteria.IsNull)
      {
        criteria.Value = null;
      }
      else if (string.IsNullOrEmpty(criteria.Value))
      {
        throw new EPiServerException("The crieria value cannot be null or empty. Set the IsNull property to search for null.");
      }
      ...
    }

因此,如果不将 IsNull 设置为 true,则无法搜索空字符串值。然后将其反馈给 EPiServer.DataAccess.PropertySearchDB.ExecuteCriteria 方法,该方法构造和格式化 DB 命令。因为 IsNull 为真,所以使用 netPropertySearchNull 存储过程。搜索字符串需要使用 netPropertySearchString 存储过程。

  if (criteria.IsNull && !PageDB.IsMetaData(criteria.Name))
  {
    cmd.CommandText = "netPropertySearchNull";
  }

我的建议是加载完整的页面列表并使用 linq 进行过滤。或者,您可以考虑绕过 API 并实现直接 DB 查询,或使用一些低级 EPiServer 数据访问方法(不推荐)

为我的第一个答案道歉 - 我真的应该在发布之前测试代码:)

于 2012-01-16T12:23:25.537 回答
0

我已经看到页面有一个隐藏的布尔属性“属性 X 包含一个值”,该属性是在保存事件中设置的。

然后将该 bool 属性用作 PropertyCriteria,而不是您真正感兴趣的那个。

于 2012-08-23T07:13:26.343 回答
-2

要查找空值,您需要在 PropertyCriteria 上指定IsNull属性并使用 Equal 比较条件。

例如

var criterias = newPropertyCriteriaCollection
{
  new PropertyCriteria()
  { 
    Condition = CompareCondition.NotEqual, 
    Name = "MyProperty", 
    IsNull = true, 
    Type = PropertyDataType.String
  }
};
于 2012-01-12T09:28:37.623 回答