title: Entity Framework Core - Sorting on Calculated Field
body: <p>I have a model similar to this:</p>
  <pre><code>Public class Item {
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime? StartTime { get; set; }
    public DateTime? EndTime { get; set; }
    public int? TotalTime
    {
      get
      {
        return Convert.ToInt32(EndTime.Value - StartTime.Value).TotalMinutes);
      }
    }
    etc...
  </code></pre>
  <p>Then I get a list of Items:</p>
  <pre><code>IQueryable&lt;Items&gt; items = itemsContext.Items.Where(e =&gt; e.Id &gt; 0);
  </code></pre>
  <p>I do some calculations, run some other code, then at the end I want to return the list of Items ordered by Name ASC and TotalTime DESC. This is what I tried:</p>
  <pre><code>items = items.OrderBy(e =&gt; e.Name).ThenByDescending(e =&gt; e.TotalTime);
  </code></pre>
  <p>But I get an error: <em>&quot;The LINQ expression could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().&quot;</em></p>
  <p>I also tried this, but get the same error:</p>
  <pre><code>items = items.OrderBy(e =&gt; e.Name).ThenByDescending(e =&gt; e.TotalTime ?? 0);
  </code></pre>
  <p>How can I sort my data set by TotalTime?</p>