title: EF Core 3.0 conditional order by in LINQ when the condition is validated in DB
body: <p>The following is a fairly simple example of the issue that I need to solve. I am using <code>EF Core 3.0</code> and with <code>LINQ</code>, I need to get all the guys from the database in a specific order. First I need all guys where it has a <code>Feature class</code> and the <code>Until</code> property is bigger than <code>DateTime.Now</code> ordered by the <code>At</code> property descending and then ordered by the <code>Id</code> property from the <code>Guy class</code> descending.</p>
  <pre><code>public class Guy
  {
      public int Id {get;set;}
      public Feature Feature {get;set;}
  }
  public class Feature
  {
      public int Id {get;set;}
      public DateTime? At {get;set;}
      public DateTime? Until {get;set;}
  }
  
  _dbcontext.Guys.Add(new Guy{Id = 1, Feature = new Feature {At = DateTime.Now.AddDays(-1), Until = DateTime.Now.AddDays(7)}})
  _dbcontext.Guys.Add(new Guy{Id = 2, Feature = new Feature {At = DateTime.Now.AddDays(3), Until = DateTime.Now.AddDays(7)}})
  _dbcontext.Guys.Add(new Guy{Id = 3})
  _dbcontext.Guys.Add(new Guy{Id = 4})
  </code></pre>
  <p>Example <code>_dbcontext.Guys.Include(x =&gt; x.Feature).OrderByDescending(x =&gt; x.Feature.Until &gt; DateTime.Now ? x.Feature.At : DateTime.Now).ThenByDescending(x =&gt; x.Id).ToList();</code>
  should return Guy with <code>Id 1</code> first as <code>Feature.At</code> property is greater than <code>DateTime.Now</code> and the rest should be sorted by the <code>Id</code> property descending. <code>Id 4</code>,
  <code>Id 3</code> then <code>Id 1</code>.</p>
  <p>Unfortunately, this example sort of works, but the issue is, first it will return the ones which has no <code>Feature class</code> (ordered descending by the <code>Id</code> property), then the ones which has a <code>Feature class</code> where the <code>At</code> is greater than <code>DateTime.Now</code>. Again these are ordered by the <code>Until</code> property correctly, but these should be on top.</p>
  <p>Any help will be much appreciated.</p>