Hierarchy Problem -> Replace Recursion with Linq Join?

后端 未结 4 1320
离开以前
离开以前 2020-12-16 07:45

I have a self referential table, which has ID, ParentID (nullable).

So, the table contains many nodes, each node could be the root in the hierarchy (parent is null),

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-16 08:19

    If you want to select all direct children of a node, a simple query like the following should do the job:

    from item in table
    where item.ID == parentID;
    select item
    

    If you want to select all descendants of a node, this is not possible with LINQ, because it requires recursion or a stack which LINQ (and SQL) doesn't provide.

    See also:

    • StackOverflow: LINQ to SQL for self-referencing tables?
    • CodeProject: T-SQL - How to get all descendants of a given element in a hierarchical table
    • StackOverflow: Expressing recursion in LINQ

提交回复
热议问题