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.
I have the following classes:
public class Product
{
public long Id {get;set;}
public string Name {get;set;}
public List<Specification> Specifications {get;set;}
}
public class Specification
{
public long Id {get;set;}
public long TypeId {get;set;}
public string TextValue {get;set;}
}
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)
My list of products looks like this:
IQueryable<Product> productQuery = _context.Products.Include("Specifications");
SortTypeId is the type of specification I want to order the Product list on.
This is what I tried to do:
productQuery = productQuery
.OrderBy(pq => pq.Specifications
.OrderBy(s => s.TypeID == SortTypeID ? Int32.MinValue : s.Id)
.ThenBy(v => v.TextValue));
This gives the following exception:
System.ArgumentException: 'DbSortClause expressions must have a type that is order comparable.
Parameter name: key'
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).