如何在代码中获取查询预览列表果园1.7 [英] How to get the Query preview list in code | Orchard 1.7

查看:71
本文介绍了如何在代码中获取查询预览列表果园1.7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CMS中有一个自定义内容类型"SideBarLinks".我创建了一个查询来获取以下类型的项目的列表:"SideBarLinks",并指定了排序和过滤条件.在CMS中预览查询结果时,此方法工作正常.

There is a Custom Content Type "SideBarLinks" in CMS. I created a query to get the list of items of this type: "SideBarLinks" and specified sort and filter criteria as well. This works fine when Previewing the query results in CMS.

有没有办法我可以在代码中获得相同的列表,例如在任何Controller的Action方法中?可查询果园的哪些类/服务?以及如何在Controller方法中执行所有这些过滤,查询结果排序?

Is there a way i can get the same list in code, say in the Action method of any Controller ? Which Class/Services of Orchard is there to query ? And how to perform all those filtering, sorting of the Query results in a Controller method ?

到目前为止,我曾经使用以下代码查询内容项,这些代码是我通过Blog和其他论坛引用的.完全归功于他们.这似乎初始化IOrchardServices并使用此服务获取项目.

So far I used to query Content items using below codes i have referred through Blogs and other Forum. Full credit to them. This seems to initialize the IOrchardServices and using this service get the items.

public IOrchardServices Services { get; private set; }
public AdminController(IOrchardServices services, IShapeFactory shapeFactory) {
        Services = services;

        T = NullLocalizer.Instance;
        Logger = NullLogger.Instance;
        Shape = shapeFactory;
    } 

这是操作方法:

public ActionResult List(int id) {
            List<ContentItem> query = Services.ContentManager.GetAllVersions(id).ToList();
            if (!query.Any()) {
                return HttpNotFound();
            }

            return View(query);
        } 

如何获取特定于查询的项目?

How to get items specific to a Query ?

推荐答案

这是一个相当广泛的问题.我肯定会说,最好的起点是源代码,答案几乎总是源代码中.

That's a pretty broad question. I would definitely say the best place to start is in the source code, the answer is almost always in the source.

但是这里.您需要的第一件事是内容管理器.

But here goes. The first thing you need is the content manager.

private readonly IContentManager _contentManager;
public MyController(IContentManager contentManager) {
    _contentManager = contentManager;
}

获取特定内容类型的列表

To get a list of a specific content type

_contentManager.Query(VersionOptions.Published, "SideBarLinks").List();

按特定部分获取内容项列表

To get a list of content items by a specific part

_contentManager.Query<UserPart, UserPartRecord>().List();

要获取列表并对其进行过滤

To get a list and filter it

_contentManager.Query<UserPart, UserPartRecord>().ForVersion(VersionOptions.Latest).Where(e => e.UserName = "Bob").List();

按其他部分过滤

_contentManager.Query<UserPart, UserPartRecord>()
    .Join<CommonPartRecord>()
    .Where(e => e.CreatedUtc > DateTime.Now.AddDays(-1))
    .List();

要在查询期间加载更多数据,我们稍后将需要显示

To load more data during the query that we will need later for displaying

_contentManager.Query<UserPart, UserPartRecord>()
    .WithQueryHints(new QueryHints().ExpandParts<ExtraUserPart>())
    .List();

我认为对Orchard的工作方式有一个体面的理解对于建立查询很重要. Orchard是一个非常懒惰的系统,如果没有通知,它不会去加入记录表.而且由于它可以以XML存储部分(如果告诉您)和现场数据的事实,除非您需要过滤或排序数据,否则您无需担心任何事情,Orchard拥有了您需要的所有数据.如果确实将数据存储在记录类中以供以后访问,那么Orchard将不得不稍后延迟加载它们,从而导致更多的数据库查询和严重的性能损失.

I think a decent understanding of how Orchard works is important for building queries. Orchard is a very lazy system, it just wont go and join record tables if it isn't told to. And due to the fact that it can store part (if you tell it to) and field data in XML, unless you need to filter or order your data, you don't need to worry about anything, Orchard has all the data you need. If you do have data stored in record classes that you want to access later, Orchard will have to lazily load it later, resulting in more db queries and severe performance loss.

这篇关于如何在代码中获取查询预览列表果园1.7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆