Nhibernate将ICriteria迁移到QueryOver [英] Nhibernate migrate ICriteria to QueryOver
本文介绍了Nhibernate将ICriteria迁移到QueryOver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们正在使用ICriteria
,现在我们希望在Nhibernate中切换到更具可读性的QueryOver
We are using ICriteria
and now we would like to switch to more readable QueryOver
in Nhibernate
有人可以给我提示如何将Icriteria的通用分页逻辑转换为QueryOver吗?
Can someone give me a hint how to convert this generic pagination logic for Icriteria to QueryOver?
public static PagedList<T> PagedList<T>(this ICriteria criteria,
ISession session, int pageIndex, int pageSize) where T : class
{
if (pageIndex < 0)
pageIndex = 0;
var countCrit = (ICriteria)criteria.Clone();
countCrit.ClearOrders(); // so we don’t have missing group by exceptions
var results = session.CreateMultiCriteria()
.Add<long>(countCrit.SetProjection(Projections.RowCountInt64()))
.Add<T>(criteria.SetFirstResult(pageIndex * pageSize).SetMaxResults(pageSize))
.List();
var totalCount = ((IList<long>)results[0])[0];
return new PagedList<T>((IList<T>)results[1], totalCount, pageIndex, pageSize);
}
推荐答案
我的使用方式:
var session = ... // get a ISession
// the QueryOver
var query = session.QueryOver<MyEntity>();
// apply all filtering, sorting...
query...
// GET A ROW COUNT query (ICriteria)
var rowCount = CriteriaTransformer.TransformToRowCount(query.UnderlyingCriteria);
// ask for a list, but with a Future, to combine both in one SQL statement
var list = query
.Future<MyEntity>()
.ToList();
// execute the main and count query at once
var count = rowCount
.FutureValue<int>()
.Value;
// list is now in memory, ready to be used
var list = futureList
.ToList();
因此,我们使用QueryOver
,并从基础条件和转换器中获利.使用 Future
,我们还可以一次执行所有命令.
So, we are using QueryOver
, and profiting from underlying criteria and transformer. With a Future
, we also execute that all at one command.
这篇关于Nhibernate将ICriteria迁移到QueryOver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文