title: LINQ Order by column x and if empty take column y
body: <p>I'm building a simple messaging system with the following data model:</p>
  
  <pre><code>public partial class Conversation
  {
      public Conversation()
      {
          this.Messages = new HashSet&lt;Message&gt;();
          this.Customers = new HashSet&lt;Customer&gt;();
          this.MessagingHubConnections = new HashSet&lt;MessagingHubConnection&gt;();
      }
  
      public int Id { get; set; }
      public int BoatId { get; set; }
      public System.DateTime TimeCreated { get; set; }
  
      public virtual ICollection&lt;Message&gt; Messages { get; set; }
      public virtual Boat Boat { get; set; }
      public virtual ICollection&lt;Customer&gt; Customers { get; set; }
      public virtual ICollection&lt;MessagingHubConnection&gt; MessagingHubConnections { get; set; }
  }
  
  public partial class Message
  {
      public int Id { get; set; }
      public int ConversationId { get; set; }
      public string Text { get; set; }
      public bool IsRead { get; set; }
      public System.DateTime TimeSend { get; set; }
      public int CustomerId { get; set; }
  
      public virtual Conversation Conversation { get; set; }
      public virtual Customer Customer { get; set; }
  }
  </code></pre>
  
  <p>When a customer opens his account dashboard, I want to display a list of all the conversations. This should be ordered according to the following rule: The first conversation in the list is the one with a message with the latest <code>Message.TimeSent</code>. If the conversation has no messages, it has to pick the <code>Conversation.TimeCreated</code>.
  The code below is what I have right now but this is obviously not working when a Conversation has no messages. The variable <code>conversations</code> in the code below is an <code>IQueryable&lt;Conversation&gt;</code>.</p>
  
  <pre><code>var orderedConversations = conversations.OrderByDescending(c =&gt; c.Messages.Max(m =&gt; m.TimeSend));
  </code></pre>
  
  <p>Anyone who can help me out?</p>