views:

585

answers:

4

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), or any level of the hierarchy (parent exists elsewhere in the table).

Given an arbitrary starting node, is there an elegant linq query that will return all children of the hierarchy from that node?

Thanks.

+2  A: 

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:

dtb
ya, I want all descendants no matter what level of the hierarchy and was hoping that linq was smart enough to handle that, using joins or let statements or some other magic
Scott
The LINQ query you proposed to get all the direct children does not work. A Join or a SelectMany would do the job, but as you have suggested, not recursively.
Yannick M.
+2  A: 

Here is a quick one I just wrote:

class MyTable
{
    public int Id { get; set; }
    public int? ParentId { get; set; }
    public MyTable(int id, int? parentId) { this.Id = id; this.ParentId = parentId; }
}

List<MyTable> allTables = new List<MyTable> {
    new MyTable(0, null), 
    new MyTable(1, 0),
    new MyTable(2, 1)
};

Func<int, IEnumerable<MyTable>> f = null;
f = (id) =>
{
    IEnumerable<MyTable> table = allTables.Where(t => t.Id == id);

    if (allTables
        .Where(t => t.ParentId.HasValue && t.ParentId.Value == table
            .First().Id).Count() != 0)
        return table
            .Union(f(
            allTables.Where(t => t.ParentId.HasValue && t.ParentId.Value == table
                .First().Id).First().Id));
    else return table;

};

But I believe it is possible to do using SQL with a Union ALL.

Yuriy Faktorovich
Don't believe it is possible in SQL without hacking up a procedure that is called recursively
Yannick M.
A: 

Basically I'm going with something like this as discussed in the SO link you proivded.

public IQueryable GetCategories(Category parent)
{
    var cats = (parent.Categories);
    foreach (Category c in cats )
    {
        cats  = cats .Concat(GetCategories(c));
    }
    return a;
}

CTEs are probably the best solution but I'd like to keep things all in the same tier for now.

Scott
+1  A: 

I know this is an old post but you should check out this extension:

http://www.scip.be/index.php?Page=ArticlesNET23

I've been using it and it is working great.

rboarman