title: Insert Linq query part inside existing query
body: <p>Having the following variable which stores an <code>IQueryable</code>:</p>
  
  <pre><code>var mainQuery = session
      .Query&lt;Employee&gt;()
      .Select(e =&gt; new
      {
          e.Name,
          e.Address
      });
  </code></pre>
  
  <p>And a method which takes that IQueryable as parameter.</p>
  
  <pre><code>public DataTable GetData(IQueryable query)
  {
      ...
  }
  </code></pre>
  
  <p>How can I write code inside <code>GetData()</code> that adds <code>OrderBy()</code> before the <code>Select()</code>?</p>
  
  <p>Final value of <code>query</code> should look like it was built using following Linq expression:</p>
  
  <pre><code>var query = session
      .Query&lt;Employee&gt;()
      .OrderBy(e =&gt; e.Age)
      .Select(e =&gt; new
      {
          e.Name,
          e.Address
      });
  </code></pre>
  
  <p>This is required because if I add the <code>OrderBy()</code> after <code>Select()</code> I will only be able to sort by the members of the anonymous type (name, address). If an employee would also have and age, I could not sort by it when placing <code>OrderBy()</code> after <code>Select()</code>.
  Thanks for your help!</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p><code>GetData</code> has no knowledge of the structure of the query except that it ends with a <code>Select</code>.
  And it has to have that exact signature <code>public DataTable GetData(IQueryable query)</code> - no extra parameters.</p>
  
  <p>What is required it to modify the existing query inside the method and add <code>OrderBy</code> before the <code>Select</code>.</p>
  
  <p>When there will be a correct answer I will accept and vote for it.</p>