本文介绍了一次调用即可得到多个结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在分页数据时,我不仅要返回10个结果,而且还希望获得所有页面中的项目总数.

When paging data, I want to not only return 10 results, but I also want to get the total number of items in all the pages.

如何在一次调用中获得页面的总数和结果?

How can I get the total count AND the results for the page in a single call?

我的分页方法是:

public IList GetByCategoryId(int categoryId,int firstResult,int maxResults) {

public IList GetByCategoryId(int categoryId, int firstResult, int maxResults) {

        IList<Article> articles = Session.CreateQuery(
            "select a from Article as a join a.Categories c where c.ID = :ID")
            .SetInt32("ID", categoryId)
            .SetFirstResult(firstResult)
            .SetMaxResults(maxResults)
            .List<Article>();



        return articles;
    }

推荐答案

事实是您打了两次电话.但是count(*)调用在大多数数据库中非常便宜,而在主调用之后执行count(*)调用有时会帮助查询缓存.

The truth is that you make two calls. But a count(*) call is very, very cheap in most databases and when you do it after the main call sometimes the query cache helps out.

您的计数器调用通常也会有所不同,它实际上不需要使用内部联接才有意义.还有一些其他小的性能调整,但是大多数时候您不需要它们.

Your counter call will often be a little different too, it doesn't actually need to use the inner joins to make sense. There are a few other little performance tweaks too but most of the time you don't need them.

这篇关于一次调用即可得到多个结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 17:22