我有这个实体模型:
public class ProductCategory
{
public int Id { get; set; }
public string Title { get; set; }
public int SortOrder { get; set; }
public int? ParentId { get; set; }
public ProductCategory ParentCategory { get; set; }
public ICollection<ProductCategory> Children { get; set; } = new List<ProductCategory>();
}
如何编写正确排序类别的递归树结构的LINQ查询?这就是我现在所拥有的:
var categories = _context.ProductCategories
.Include(e => e.Children).ToList().OrderBy(o => o.SortOrder)
.Where(e => e.ParentId == null)
.OrderBy(o => o.SortOrder);
根类别(ParentId == null
已正确排序,但子项不是。