Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NHibernate-动态查询覆盖参数_C#_Asp.net Mvc_Nhibernate - Fatal编程技术网

C# NHibernate-动态查询覆盖参数

C# NHibernate-动态查询覆盖参数,c#,asp.net-mvc,nhibernate,C#,Asp.net Mvc,Nhibernate,我有一个MVC项目,我正试图为它设置一个很好的分页解决方案 我在SQL Server中有几个表,它们有几千行,我希望能够逐页浏览。如果我没有任何类型的过滤器应用于分页,那么它工作得很好。这是我用来做这件事的方法: public virtual PagedList<T> GetPagedData(int startIndex, int count) { var rowCount = session.CreateCriteria(typeof(T)).SetPro

我有一个MVC项目,我正试图为它设置一个很好的分页解决方案

我在SQL Server中有几个表,它们有几千行,我希望能够逐页浏览。如果我没有任何类型的过滤器应用于分页,那么它工作得很好。这是我用来做这件事的方法:

    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);
    }
public虚拟页面列表GetPagedData(int startIndex,int count){
var rowCount=session.CreateCriteria(typeof(T)).setprojections(Projections.rowCount()).FutureValue().Value;
var pageOfItems=session.CreateCriteria(typeof(T)).SetFirstResult(startIndex).SetMaxResults(count).List();
返回新的PagedList(pageOfItems、startIndex、count、rowCount);
}
我还希望能够传入查询以进一步缩小结果范围,并返回一个新的分页表。到目前为止,我得到的是:

    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);
    }
public virtual PagedList GetPagedData(int startIndex,int count,System.Linq.Expressions.Expression谓词),其中T:class{
var rowCount=session.QueryOver().Where(谓词).Select(Projections.rowCount()).FutureValue().Value;
var pageOfItems=session.QueryOver().Where(谓词).Skip(startIndex.Take(计数).List();
返回新的PagedList(pageOfItems、startIndex、count、rowCount);
}
对此的调用如下所示:

networks = mRepository.GetPagedData<Network>(page ?? 1, pageSize, x => x.Name.Contains(q));
networks=mRepository.GetPagedData(第1页,pageSize,x=>x.Name.Contains(q));
问题是,它不喜欢“Contains”表达式。如果我进行精确匹配,它工作正常(x.Name==q),但我没有得到我想要的结果

我在使用“Contains”时看到的例外情况是:

无法识别的方法调用:System.String:布尔包含(System.String)

有人知道如何让它动态地接受这样的表达式吗?我有一个基本的存储库类,我把它放在其中,因为我将对其他几个表使用相同类型的行为。我可以为每个表编写一个单独的方法,但如果可能的话,我宁愿动态地编写


谢谢你的建议

QueryOver不是LINQ,它只接受非常有限的一组表达式

我的建议是,通过将
QueryOver
替换为
Query
来重写使用LINQ的方法。唯一的问题是,使用将来的查询进行计数更困难(有关详细信息,请参阅)

这是一个没有未来的版本:

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);
}
公共虚拟页面列表GetPagedData(int startIndex,int count,
表达式谓词),其中T:class
{
var query=session.query().Where(谓词);
var rowCount=query.Count();
var page=query.Skip(startIndex).Take(count).List();
返回新的PagedList(pageOfItems、startIndex、count、rowCount);
}

使用
查询,您必须使用扩展方法
IsLike
,因此
x.Name.IsLike(q,MatchMode.Anywhere)

谢谢您的建议。这很好用。我将研究这篇文章以了解未来的查询用法,但我很高兴它能在这段时间内发挥作用。