题
例如,我想到填入内在ASP.NET 网页只有数据所必要的#行的显示。怎么可能它能够支持这个吗?
解决方案
ICriteria
有一个 SetFirstResult(int i)
方法,这表明该索引的第一个项目,你希望得到的(基本数据第一行,在你页)。
它也有一个 SetMaxResults(int i)
方法,其指示的行数您想要获得的(即,你的页面尺寸)。
例如,这种标准的目的得到第10结果的数据格:
criteria.SetFirstResult(0).SetMaxResults(10);
其他提示
你还可以利用期货的功能,在它能够执行查询,以获得的总记录数以及实际的结果在一个单一的查询。
例
// Get the total row count in the database.
var rowCount = this.Session.CreateCriteria(typeof(EventLogEntry))
.Add(Expression.Between("Timestamp", startDate, endDate))
.SetProjection(Projections.RowCount()).FutureValue<Int32>();
// Get the actual log entries, respecting the paging.
var results = this.Session.CreateCriteria(typeof(EventLogEntry))
.Add(Expression.Between("Timestamp", startDate, endDate))
.SetFirstResult(pageIndex * pageSize)
.SetMaxResults(pageSize)
.Future<EventLogEntry>();
得到总的记录数,必做到以下几点:
int iRowCount = rowCount.Value;
一个良好的讨论什么期货给你的是 在这里,.
从它能够3及以上的,可以使用 QueryOver<T>
:
var pageRecords = nhSession.QueryOver<TEntity>()
.Skip((PageNumber - 1) * PageSize)
.Take(PageSize)
.List();
你可能还需要明确了你的结果是这样的:
var pageRecords = nhSession.QueryOver<TEntity>()
.OrderBy(t => t.AnOrderFieldLikeDate).Desc
.Skip((PageNumber - 1) * PageSize)
.Take(PageSize)
.List();
public IList<Customer> GetPagedData(int page, int pageSize, out long count)
{
try
{
var all = new List<Customer>();
ISession s = NHibernateHttpModule.CurrentSession;
IList results = s.CreateMultiCriteria()
.Add(s.CreateCriteria(typeof(Customer)).SetFirstResult(page * pageSize).SetMaxResults(pageSize))
.Add(s.CreateCriteria(typeof(Customer)).SetProjection(Projections.RowCountInt64()))
.List();
foreach (var o in (IList)results[0])
all.Add((Customer)o);
count = (long)((IList)results[1])[0];
return all;
}
catch (Exception ex) { throw new Exception("GetPagedData Customer da hata", ex); }
}
当呼数据是有另一种方法得到输入的结果,从多准则或每个人都不会相同,只是喜欢我吗?
感谢
最有可能在内你会想要展示的一片的数据加上行的总数(rowcount)的总量数据匹配你的查询。
你应该使用MultiQuery发送两个选择计数(*)查询。SetFirstResult(n)。SetMaxResult(m)查询数据库在一个单一的呼吁。
注意到其结果将是一个列表,持有2列出,一个用于数据的切片和一个用于计数。
例如:
IMultiQuery multiQuery = s.CreateMultiQuery()
.Add(s.CreateQuery("from Item i where i.Id > ?")
.SetInt32(0, 50).SetFirstResult(10))
.Add(s.CreateQuery("select count(*) from Item i where i.Id > ?")
.SetInt32(0, 50));
IList results = multiQuery.List();
IList items = (IList)results[0];
long count = (long)((IList)results[1])[0];
我建议你创建一个特定的结构来处理分页。喜欢的东西(我Java程序,但是这应该很容易地图):
public class Page {
private List results;
private int pageSize;
private int page;
public Page(Query query, int page, int pageSize) {
this.page = page;
this.pageSize = pageSize;
results = query.setFirstResult(page * pageSize)
.setMaxResults(pageSize+1)
.list();
}
public List getNextPage()
public List getPreviousPage()
public int getPageCount()
public int getCurrentPage()
public void setPageSize()
}
不隶属于 StackOverflow