Linq 在搜索中吐出数字列表(kentico)

Linq spit numeric list in search (kentico)

在 kentico 中,获取文档的标准方式如下(我相信它基于 ObjectQuery 并具有 linq 命令)。我正在尝试通过另一个包含“1|2|3”等数据的字段 "newsCategory" 对其进行过滤。所以我不能添加 .Search("newsCategory", 1) 等,因为我需要先拆分列表才能搜索它。我应该看什么方向? select 子查询? (我是 linq 的新手)

// Get documents
var news = DocumentHelper.GetDocuments("CMS.News")
.OnSite("CorporateSite")
.Path("/News", PathTypeEnum.Children)
.Culture("en-us")
.CombineWithDefaultCulture(false);         

我相信这样的事情会奏效。有一个where属性应该可以把值拉出来。不确定它会如何处理先是 1 然后是 11 的情况,但它可能正在研究中。

// Get documents
var news = DocumentHelper.GetDocuments("CMS.News")
.OnSite("CorporateSite")
.Path("/News", PathTypeEnum.Children)
.Culture("en-us")
.CombineWithDefaultCulture(false)
.WhereIn("NewsCategory",1);

你确定你的数据是 1|2|3 而不是 1|2|3|或者 |1|2|3 ?

如果是,你可以.Where("NewsCategory", QueryOperator.Like, "%" + id + "|%")

否则,您可能需要返回更多结果,然后遍历它们并拆分值以找到您想要的确切结果。

编辑: 查看 this article,其中显示了一些更高级的 where 命令,您可以将这些命令用于数据查询 API。您应该能够使用这些选项为 MacGyver 提供适当的过滤器。

我相信您正在寻找:

.WhereLike("DocumentCategoryID", "CategoryID");
//OR
.WhereLike("DocumentCategory","CategoryName");

我没有安装 v8 来仔细检查要过滤的确切 key/value 对,但根据 this Document Query API article,您使用 WhereLike() 方法过滤文档集。

根据API documentation, GetDocuments() returns a MultiDocumentQuery object。我不是 100% 确定它是否实现了 IEnumerable,因此您甚至可能无法将 LINQ 与它一起使用。

由于这是来自耦合 table 的字段,您不能通过 属性 访问它,而必须使用 GetValue()。获得后,您可以像使用常规字符串一样使用它:

var news = DocumentHelper.GetDocuments("CMS.News")
            .OnSite("CorporateSite")
            .Path("/News", PathTypeEnum.Children)
            .Culture("en-us")
            .CombineWithDefaultCulture(false)
            .Where(d => d.GetStringValue("newsCategory","").Split('|').Contains("1"));