【问题标题】:Alternative to ToString() for an index search ( C# converted to SQL )用于索引搜索的 ToString() 替代方案(C# 转换为 SQL)
【发布时间】:2015-07-10 03:52:31
【问题描述】:

我目前在 Sitecore (Sitecore 8 Update 2) 项目中有以下内容:

var index = ContentSearchManager.GetIndex("sitecore_web_index");
IQueryable<SearchResultItem> queryCalls = index.CreateSearchContext().GetQueryable<SearchResultItem>().Where(item =>
                 item.TemplateName == callTemplateName &&
                 item.Path.StartsWith(callStartingPath) && 
                 item.Language == Sitecore.Context.Language.Name &&
                 item.Fields["appliedthemes"].ToString().Contains(themeID))

这应该给我某个路径下的所有项目,并以某种语言使用某个模板名称(工作正常)。 最后一行确保只返回带有特定标签的项目。

但是,我似乎无法在最后一条语句中使用 ToString() 方法,因为它无法转换为 SQL。但是我找不到其他的方法来写这个。

编辑:

错误

Server Error in '/' Application.
The method 'ToString' is not supported. Declaring type: System.Object
Description: An unhandled exception occurred.

Exception Details: System.NotSupportedException: The method 'ToString' is not supported. Declaring type: System.Object

【问题讨论】:

  • wt 是你得到的错误吗?
  • item.Fields["appliedThemes"] 返回什么数据类型?
  • Sitecore.Data.Fields.Field
  • 试试 item.Fields["appliedthemes"].Value.ToString()
  • 您能否明确说明应用的方式

标签: c# sql sitecore iqueryable


【解决方案1】:

这不是 LINQ to SQL,因此查询不会转换为 SQL,而是转换为您的底层搜索提供程序(Lucene、Solr、Coveo 等)的查询。

如您所见,不支持调用.ToString(),但也不需要。可以调用索引器直接获取字段值:

var index = ContentSearchManager.GetIndex("sitecore_web_index");
using (var context = index.CreateSearchContext()) {
    IQueryable<SearchResultItem> queryCalls = context.GetQueryable<SearchResultItem>().Where(item =>
             item.TemplateName == callTemplateName &&
             item.Path.StartsWith(callStartingPath) && 
             item.Language == Sitecore.Context.Language.Name &&
             item["appliedthemes"].Contains(themeID))
}

Item 项目类有一个索引器,它将返回字段的字符串值。如果项目上不存在该字段,它还具有返回空字符串的好处(而如果该字段不存在,则调用 item.Fields["Field Name"].Value 会导致 NullReference 异常)。

【讨论】:

    【解决方案2】:

    假设appliedthemes 字段是单行、多行或富文本字段,我建议您创建自己的继承自SearchResultItem 的类。

    public class TaggedItem : SearchResultItem 
    {
        [IndexField("appliedthemes")]
        public string AppliedThemes { get; set; }
    }
    

    如果您的字段恰好是多值字段,您可以使用IEnumerable&lt;T&gt; 属性来表示值。无论哪种方式,既然您已经定义了自己的类,您可以按如下方式使用它:

    var index = ContentSearchManager.GetIndex("sitecore_web_index");
    using (var context = index.CreateSearchContext())
    {
        var queryCalls = context.GetQueryable<TaggedItem>()
                .Where(item => item.TemplateName == callTemplateName)
                .Where(item => item.Path.StartsWith(callStartingPath))
                .Where(item => item.Language == Sitecore.Context.Language.Name)
                .Where(item => item.AppliedThemes.Contains(themeID));
    
        //...
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-22
      • 2010-09-07
      • 2014-07-12
      • 1970-01-01
      • 1970-01-01
      • 2015-12-08
      • 1970-01-01
      • 2011-07-20
      • 1970-01-01
      相关资源
      最近更新 更多