Nhibernate migrate ICriteria to QueryOver

时光毁灭记忆、已成空白 提交于 2020-01-05 05:41:47

问题


We are using ICriteria and now we would like to switch to more readable QueryOver in Nhibernate

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);
}

回答1:


The way I am using it:

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();

So, we are using QueryOver, and profiting from underlying criteria and transformer. With a Future, we also execute that all at one command.



来源:https://stackoverflow.com/questions/40446487/nhibernate-migrate-icriteria-to-queryover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!