NHibernate 3 paging and determining the total number of rows

后端 未结 3 1143
生来不讨喜
生来不讨喜 2021-01-31 06:06

I have read somewhere (can\'t remeber where and how) that NHibernate 3 allows the determination of the total number of records whilst performing a paged query (in one database q

3条回答
  •  别那么骄傲
    2021-01-31 06:53

    I don't have enough reputation to comment on CodeProgression's solution above...but the proper ONE DB call using QueryOver w/ Future<> is:

    var query = session.QueryOver()
        .Skip((Page - 1) * PageSize)
        .Take(PageSize)
        .Future();
    // var result = query.ToList();
    var rowcount = session.QueryOver()
        .Select(Projections.Count(Projections.Id()))
        .FutureValue().Value;
    var result = query.ToList();
    int iRowCount = rowcount.Value();
    

    Once you execute the .ToList() - It will hit the database. So you'd have to hit the database again to get the rowCount...Which defeats the purpose of Future<>. Do your ToList() AFTER you've done all your .Future<> queries.

提交回复
热议问题