title: LINQ to SQL lambda exp. OrderBy, Case When
body: <p>Going to need your help on this one.</p>
  
  <p>I'm trying to OrderBy first reply datetime if present. If it's empty/null, it must order by topic datetime. I've ended up with the following expression, but it just doesn't seem to work :(</p>
  
  <pre><code>return db.Topics
  .Where(t =&gt; t.ForumID == id)
  .OrderBy(
      t =&gt; t.Replies
      .OrderBy(r =&gt; r.AddDatetime.Equals(System.Data.SqlTypes.SqlDateTime.Null.Value) ? t.AddDatetime : r.AddDatetime)
      .Select(r =&gt; r.AddDatetime)
      .First()
  );
  </code></pre>