title: NHibernate & LINQ: Using Fetch() with custom ToPagedList method
body: <p>I have a problem with using the nHibernate Fetch() (or FetchMany()) method with my paging method which uses Futures to get the information it needs. And I'm not sure how to fix it but I know exactly what it's doing. Let me explain what I have so far.</p>
  
  <p>I'm using an extension method found <a href="http://sessionfactory.blogspot.com/2011/02/getting-row-count-with-future-linq.html" rel="nofollow">here</a> to let me get a row count as a Future value. it looks like this.</p>
  
  <pre><code>public static IFutureValue&lt;TResult&gt; ToFutureValue&lt;TSource, TResult&gt;(this IQueryable&lt;TSource&gt; source, Expression&lt;Func&lt;IQueryable&lt;TSource&gt;, TResult&gt;&gt; selector) where TResult : struct
          {
              var provider = (NhQueryProvider)source.Provider;
              var method = ((MethodCallExpression)selector.Body).Method;
              var expression = Expression.Call(null, method, source.Expression);
              return (IFutureValue&lt;TResult&gt;)provider.ExecuteFuture(expression);
          }
  </code></pre>
  
  <p>It works great, using this I can then combine getting a row count and getting my result set into a signle paging method that only generates a single database hit. My Paging method looks like this.</p>
  
  <pre><code>public static IList&lt;T&gt; ToPagedList&lt;T&gt;(this IQueryable&lt;T&gt; query, int pageIndex, int pageSize, out int count)
          {
              var futureCount = query.ToFutureValue(x =&gt; x.Count());
              var futureResults = pageIndex == 0 &amp;&amp; pageSize == 0 ? query.ToFuture() : query.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToFuture();
              count = futureCount.Value;
              return futureResults.ToList();
          }
  </code></pre>
  
  <p>With this in place I can make queries like the following.</p>
  
  <pre><code>repositoryInstance.Query().Where(x=&gt;x.SomeTextField.Contains("lake")).ToPagedList(pageIndex, pageSize, out count);
  </code></pre>
  
  <p>This much works like a charm. However now I need to be able to define certain relationships as eager loaded, in this case it might look like this.</p>
  
  <pre><code>repositoryInstance.Query().Where(x=&gt;x.SomeTextField.Contains("lake")).FetchMany(x=&gt;x.Details).ToPagedList(pageIndex, pageSize, out count);
  </code></pre>
  
  <p>This blows up with a QueryException with the description "Query specified join fetching, but the owner of the fetched association was not present in the select list". The reason for this is because of my paging method, it's trying to apply an aggregate row count on the query which has the Fetch() on it, and you can't do that, because the error is right. The owner of the fetched association wasn't in the select list (at least it isn't in one of them), my select is essentially for the first query is for a single integer. I thought maybe I could rip out the Expression nodes once in the ToPagedList that are for any fetching but I have no idea if it's possible or how ugly it would be. I'm looking for some alternatives that might let be get the behavior I want while bypassing the limitation on how Fetching works. Any thoughts?</p>