如何在实现 IFilterProvider 的过滤器的 ApplyFilter 方法中以编程方式在 Orchard 中按 属性 进行过滤

How to programmatically filter by property in Orchard in the ApplyFilter method of a Filter implementing IFilterProvider

简化模型:

public class GolfCourseDetailsPart : ContentPart<GolfCourseDetailsRecord>
{
  public bool ShowInHomePage {... //Get and Set using Retrieve and Store  methods
}

简化迁移:

ContentDefinitionManager.AlterTypeDefinition("GolfCourse", gc => gc
     //...
     .WithPart(typeof(GolfCourseDetailsPart).Name)
);

我需要过滤类型为 "GolfCourse" 的所有项目,以仅获取将 ShowInHomePage 设置为 true.

的项目

过滤器:

我创建了一个实现 IFilterProvider 接口的过滤器,它 returns 所有 GolfCourse 内容项,但我还不能通过 ShowInHomePage 进行过滤:

private void ApplyFilter(FilterContext context)
{
 context.Query = context.Query.Join(x=>x.ContentPartRecord(typeof(GolfCourseDetailsRecord))); 
}

我怎样才能通过 属性 ShowInHomePage 过滤?

您是否有任何理由要创建 IFilterProvider? 这些仅在您希望自定义过滤器可用于查询预测时才有用。

如果您只是想以编程方式获取过滤后的数据,那么我会使用 ContentManager 的 Query 方法。

这里有一组关于如何查询 Orchard 的示例,我认为它对你来说会比我简单地把你需要的查询放在这里更有用:https://orchardtrainingdemo.codeplex.com/SourceControl/latest#Controllers/ContentsAdminController.cs

您快完成了,唯一缺少的部分是 .Where 子句。在 HQL 查询中,它看起来像这样:

private void ApplyFilter(FilterContext context)
{
    context.Query = context
        .Query
        .Join(x => x.ContentPartRecord(typeof(GolfCourseDetailsRecord)))
        .Where(x => x.ContentPartRecord<GolfCourseDetailsRecord>(), g => g.Eq("ShowInHomePage", true));
}