c# - NHibernate - dynamic QueryOver parameter -


i have mvc project i'm trying setup nice paging solution for.

i have several tables in sql server have several thousand rows able page through. if don't have type of filter apply paging, works great. method i'm using 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 want ability pass in query narrow results down further, , return new paging table. have far is:

    public virtual pagedlist<t> getpageddata<t>(int startindex, int count, system.linq.expressions.expression<func<t, bool>> predicate) 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 this:

networks = mrepository.getpageddata<network>(page ?? 1, pagesize, x => x.name.contains(q)); 

the problem is, doesn't "contains" expression. if exact match, works fine (x.name == q), don't results i'm after.

the exception i'm seeing using "contains" is:

unrecognised method call: system.string:boolean contains(system.string)

does have idea how accept expression dynamically? have base repository class i've put in, because i'll use same type of behavior several other tables. write separate method each table, rather dynamically if it's possible.

thanks advice!

queryover not linq, , accepts limited set of expressions.

my suggestion rewrite method use linq, replacing queryover query. problem it's harder count using future query (see this answer details)

here's version without future:

public virtual pagedlist<t> getpageddata<t>(int startindex, int count,                expression<func<t, bool>> predicate) 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); } 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -