title: NHibernate - dynamic QueryOver parameter
body: <p>I have an MVC project that I'm trying to setup a nice paging solution for.  </p>
  
  <p>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:</p>
  
  <pre><code>    public virtual PagedList&lt;T&gt; GetPagedData(int startIndex, int count) {
          var rowCount = session.CreateCriteria(typeof(T)).SetProjection(Projections.RowCount()).FutureValue&lt;Int32&gt;().Value;
          var pageOfItems = session.CreateCriteria(typeof(T)).SetFirstResult(startIndex).SetMaxResults(count).List&lt;T&gt;();
          return new PagedList&lt;T&gt;(pageOfItems, startIndex, count, rowCount);
      }
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>    public virtual PagedList&lt;T&gt; GetPagedData&lt;T&gt;(int startIndex, int count, System.Linq.Expressions.Expression&lt;Func&lt;T, bool&gt;&gt; predicate) where T : class {
          var rowCount = session.QueryOver&lt;T&gt;().Where(predicate).Select(Projections.RowCount()).FutureValue&lt;Int32&gt;().Value;
  
          var pageOfItems = session.QueryOver&lt;T&gt;().Where(predicate).Skip(startIndex).Take(count).List&lt;T&gt;();
          return new PagedList&lt;T&gt;(pageOfItems, startIndex, count, rowCount);
      }
  </code></pre>
  
  <p>The call to this would look something like this:</p>
  
  <pre><code>networks = mRepository.GetPagedData&lt;Network&gt;(page ?? 1, pageSize, x =&gt; x.Name.Contains(q));
  </code></pre>
  
  <p>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.</p>
  
  <p>The exception I'm seeing using "Contains" is:</p>
  
  <blockquote>
    <p>Unrecognised method call: System.String:Boolean Contains(System.String)</p>
  </blockquote>
  
  <p>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.</p>
  
  <p>Thanks for any advice!</p>