title: How to sort by condition using the mongo driver in c#
body: <p>I've been following the mongo driver (2.4) documentation and am currently stuck trying to put a conditional on a sort.</p>
  
  <p>Following the documentation, I got a AsQueryable instance and used the where function with an expression passed in. That works fine until the moment I want to order by a conditional instead of a field.</p>
  
  <p>Here's what I'd like to order by:</p>
  
  <pre><code>db.child.AsQueryable().Where(expression).OrderBy(x=&gt; x.parentId == someParentId); 
  </code></pre>
  
  <p>which should be something along the lines of in linq</p>
  
  <pre><code>SELECT * FROM child
  ORDER BY
    CASE parentId
    WHEN someParentId THEN 1
    ELSE 2 END,
    parentId; 
  </code></pre>
  
  <p>Currently, however, after running the OrderBy condition I believe it's evaluating the expression because from the debugger I can see the expression change as shown below:</p>
  
  <p><a href="https://i.stack.imgur.com/UTdZm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UTdZm.png" alt="enter image description here"></a></p>
  
  <p>So the two main questions are:</p>
  
  <ol>
  <li>How do I sort by an boolean expression using the .net mongo driver.</li>
  <li>Why is the expression being converted as seen in the screenshot?</li>
  </ol>
  
  <p><a href="http://mongodb.github.io/mongo-csharp-driver/2.4/reference/driver/crud/linq/" rel="nofollow noreferrer">Mongo Driver Documentation (2.4)</a></p>