NHibernate的 - 动态QueryOver参数 [英] NHibernate - dynamic QueryOver parameter

查看:254
本文介绍了NHibernate的 - 动态QueryOver参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我试图安装一个不错的解决方案分页的MVC项目。

I have an MVC project that I'm trying to setup a nice paging solution for.

我在SQL Server中的几个表有几千行,我想通过才能页面。如果我没有任何类型的过滤器适用于寻呼,它的伟大工程。这是我使用做到这一点的方法:

I have several tables in SQL Server that have several thousand rows that I would like to be able to page through. If I don't have any type of filter to apply to the paging, it works great. This is the method I'm using to do that:

    public virtual PagedList<T> GetPagedData(int startIndex, int count) {
        var rowCount = session.CreateCriteria(typeof(T)).SetProjection(Projections.RowCount()).FutureValue<Int32>().Value;
        var pageOfItems = session.CreateCriteria(typeof(T)).SetFirstResult(startIndex).SetMaxResults(count).List<T>();
        return new PagedList<T>(pageOfItems, startIndex, count, rowCount);
    }

我也想在查询中进一步缩小下来的结果通过,并返回一个新的分页表的能力。我至今是:

I also want the ability to pass in a query to narrow the results down even further, and return a new paging table. What I have so far is:

    public virtual PagedList<T> GetPagedData<T>(int startIndex, int count, System.Linq.Expressions.Expression<Func<T, bool>> predicate) where T : class {
        var rowCount = session.QueryOver<T>().Where(predicate).Select(Projections.RowCount()).FutureValue<Int32>().Value;

        var pageOfItems = session.QueryOver<T>().Where(predicate).Skip(startIndex).Take(count).List<T>();
        return new PagedList<T>(pageOfItems, startIndex, count, rowCount);
    }

这个调用会是这个样子:

The call to this would look something like this:

networks = mRepository.GetPagedData<Network>(page ?? 1, pageSize, x => x.Name.Contains(q));

问题是,它不喜欢包含前pression。如果我这样做完全匹配,它工作正常(x.Name == Q),但我没有得到的结果我了。

The problem is, it doesn't like the "Contains" expression. If I do an exact match, it works fine (x.Name == q), but I don't get the results I'm after.

我使用包含看到的例外是:

The exception I'm seeing using "Contains" is:

未确认的方法调用:System.String:布尔包含(System.String)

Unrecognised method call: System.String:Boolean Contains(System.String)

有没有人有一个想法如何得到这个接受前pression这样的动态?我有我在把这个,因为我将使用相同​​类型的行为对其他几个表的基础库类。我可以写为每个表一个单独的方法,但我宁愿做动态的,如果它是可能的。

Does anyone have an idea how to get this to accept an expression like this dynamically? I have a base repository class that I've put this in, because I'll use the same type of behavior for several other tables. I could write a separate method for each table, but I would rather do this dynamically if it's possible.

感谢您的任何意见!

推荐答案

QueryOver不LINQ,并且只接受一组非常有限的EX pressions的。

QueryOver is not LINQ, and it only accepts a very limited set of expressions.

我的建议是,你重写你的使用LINQ的方法,用查询替换 QueryOver 。唯一的问题是,它很难用一个未来的查询做计数(见<一href=\"http://stackoverflow.com/questions/4955685/getting-count-with-nhibernate-linq-future/4968746#4968746\">this回答了解详细信息)

My suggestion is that you rewrite your method to use LINQ, by replacing QueryOver with Query. The only problem is that it's harder to do the Count using a future query (see this answer for details)

下面是一个版本,而未来:

Here's a version without Future:

public virtual PagedList<T> GetPagedData<T>(int startIndex, int count,
               Expression<Func<T, bool>> predicate) where T : class
{
    var query = session.Query<T>().Where(predicate);
    var rowCount = query.Count();
    var page = query.Skip(startIndex).Take(count).List<T>();
    return new PagedList<T>(pageOfItems, startIndex, count, rowCount);
}

这篇关于NHibernate的 - 动态QueryOver参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆