title: LINQ to SQL extension method for sorting and paging
body: <p>I have found an extension method that handles sorting and paging for <a href="http://en.wikipedia.org/wiki/Language_Integrated_Query" rel="nofollow noreferrer">LINQ</a>. While this works well, I am trying to see if there are some other ways I can use this.</p>
  
  <p>At present, the code for the <a href="http://en.wikipedia.org/wiki/Extension_method" rel="nofollow noreferrer">extension method</a> is as follows:</p>
  
  <pre><code>public static IQueryable&lt;T&gt; Page&lt;T, TResult&gt;(
    this IQueryable&lt;T&gt; obj, 
    int page, 
    int pageSize, 
    System.Linq.Expressions.Expression&lt;Func&lt;T, TResult&gt;&gt; keySelector, 
    bool asc, 
    out int rowsCount)
  {
      rowsCount = obj.Count();
  
      int innerRows = (page - 1) * pageSize;
  
      if (asc)
          return obj.OrderBy(keySelector).Skip(innerRows).Take(pageSize).AsQueryable();
      else
          return obj.OrderByDescending(keySelector).Skip(innerRows).Take(pageSize).AsQueryable();
  }
  </code></pre>
  
  <p>The method takes in an expression, which is based off the type.</p>
  
  <p>In my Dealer class, I have a method GetDealers, which essentially calls this,
  i.e. </p>
  
  <pre><code>db.User.Page(1, 2, p =&gt; p.User.UserProperty.Name, true, out rowCount)
  </code></pre>
  
  <p>From the presentation side of things though, I do not know or can access the expression as above, e.g.</p>
  
  <pre><code>ListView1.DataSource = users.GetDealers("SortColumn", pageNo, pageSize, out rowCount, bool asc);
  ListView1.DataBind();
  </code></pre>
  
  <p>The only way is to have a switch statement in my GetDealers method that would then convert to the expression. Is there a way to bypass this, or is this method OK?</p>