我正在尝试根据特定规范TypeId的TextValue通过复杂对象的子列表对复杂对象的父列表进行排序。
我有以下课程:
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;}
}
我想根据产品的特定规格对我的产品进行排序。 用例示例:根据TypeId = 3的规范的TextValue对产品进行排序(产品只能具有TypeId 3的一个规范)
我的产品列表如下:
IQueryable<Product> productQuery = _context.Products.Include("Specifications");
SortTypeId是我要在其上订购产品列表的规格类型。
这是我尝试做的事情:
productQuery = productQuery
.OrderBy(pq => pq.Specifications
.OrderBy(s => s.TypeID == SortTypeID ? Int32.MinValue : s.Id)
.ThenBy(v => v.TextValue));
这给出了以下异常:
System.ArgumentException: 'DbSortClause expressions must have a type that is order comparable.
Parameter name: key'
我还尝试通过带有Indexof的sortedProductIds列表对IQueryable进行排序,但是这也不起作用(延迟加载的IQueryable不支持IndexOf)。
答案 0 :(得分:0)
既然您说一个产品只能有一个SortTypeID
类型的规范,那么如果您在查询中按顺序加入单个规范怎么办?
这是我尝试使用查询语法建议的一些示例。
from p in _context.Products.Include("Specifications")
join orderSpec in _context.Specifications on new { ProductID = p.Id, TypeID = 3 }
equals new { ProductID = orderSpec.ProductId, TypeID = orderSpec.TypeID } into os
from orderSpec in os.DefaultIfEmpty()
orderby orderSpec != null ? orderSpec.TextValue : p.Id
select p
希望有帮助!