【发布时间】:2020-10-03 22:38:29
【问题描述】:
我正在尝试根据特定规范 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)。
【问题讨论】:
-
我认为,您可以使用
.where(x=>xSpecifications.Any(y=>y.TypeID == 3))来过滤规格,因此在过滤后您可以在产品和规格之间建立一对一的关系。并选择一个包含new {product, specification}的匿名对象并使用orderbyspecification.TextValue。
标签: c# linq sorting sql-order-by iqueryable