title: How to order a IQueryable list of parent objects by a value in it's childs list
body: <p>I'm trying to sort a parent list of complex objects by it's child list of complex objects based on the TextValue of a specific specification TypeId.</p>
  
  <p>I have the following classes:</p>
  
  <pre><code>public class Product
  {
      public long Id {get;set;}
      public string Name {get;set;}
      public List&lt;Specification&gt; Specifications {get;set;}
  }
  
  public class Specification
  {
      public long Id {get;set;}
      public long TypeId {get;set;}
      public string TextValue {get;set;}
  }
  </code></pre>
  
  <p>I want to sort my products based on a specific specification of the product.
  Example use case: Sort the products on the TextValue of the specification with TypeId = 3. (a product can only have one specification with TypeId 3)</p>
  
  <p>My list of products looks like this:</p>
  
  <pre><code>IQueryable&lt;Product&gt; productQuery = _context.Products.Include("Specifications");
  </code></pre>
  
  <p>SortTypeId is the type of specification I want to order the Product list on.</p>
  
  <p><strong>This is what I tried to do:</strong></p>
  
  <pre><code>productQuery = productQuery
  .OrderBy(pq =&gt; pq.Specifications
  .OrderBy(s =&gt; s.TypeID == SortTypeID ? Int32.MinValue : s.Id)
  .ThenBy(v =&gt; v.TextValue));
  </code></pre>
  
  <p>This gives the following exception:</p>
  
  <pre><code>System.ArgumentException: 'DbSortClause expressions must have a type that is order comparable.
  Parameter name: key'
  </code></pre>
  
  <p>I also tried to sort the IQueryable by a list of sortedProductIds with Indexof but that also didn't work (IndexOf not supported for IQueryable with lazy loading).</p>