如何在平面列表中对我的智商进行分层排序



我有一些平面列表,我想按伪层次结构排序。不使用 进行具体化。ToList((,只有智商。

我执行以下操作:

public class DictionaryElement
{
    [Key]
    public Guid ID { get; set; }
    [ForeignKey("ParentDictionaryElement")]
    public Guid? ParentDictionaryElementID { get; set; }
    public DictionaryElement ParentDictionaryElement { get; set; }
    public ICollection<DictionaryElement> DictionaryElements { get; set; }
    public String Value { get; set; }
}
 public IQueryable<DictionaryElement> Foo(DbContext db)
{
            IQueryable<DictionaryElement> allElementsQuery = db.DictionaryElements;
            //... allElementsQuery.OrderBy(); ?
            return allElementsQuery;
}

和数据:

+----+---------------------------+
| ID | ParentDictionaryElementID |
+----+---------------------------+
|  1 | null                      |
|  2 | null                      |
|  3 | 1                         |
|  4 | null                      |
|  6 | 3                         |
|  5 | 2                         |
+----+---------------------------+

我想获得将接收此类数据的IQueryable

+----+---------------------------+
| ID | ParentDictionaryElementID |
+----+---------------------------+
|  1 | null                      |
|  3 | 1                         |
|  6 | 3                         |
|  2 | null                      |
|  5 | 2                         |
|  4 | null                      |
+----+---------------------------+

不同之处在于,第二个表是一个层次结构,但处于平面、平面视图中,就像它看起来正常显示一样。也就是说,孩子紧随父母

ID 1
   ID 3 (parentID 1)
      ID 6 (parentID 3)
ID 2
   ID 5 (parentID 2)
ID 4

试试这个吗?

return allElementsQuery.OrderBy(m => m).AsQueryable();

最新更新