使用数据库内容递归填充树

时间:2012-05-16 03:34:26

标签: c# data-structures reference tree recursive-datastructures

我正在使用ORM that uses POCOs

每个表(类)都包含对其他表的引用。

public class Table1 {
    [AutoIncrement]
    public Int32 Id { get; set; }
    [Index(Unique = true)]
    public string FieldA { get; set; }
}

public Table2 {
    [AutoIncrement]
    public Int32 Id { get; set; }
    [Index(Unique = true)]
    public Table1 FieldA { get; set; }
    public int FieldB { get; set; }
}

public Table3 {
    [AutoIncrement]
    public Int32 Id { get; set; }
    [Index(Unique = true)]
    public List<Table2> FieldA { get; set; }
    [References(typeof(Table2))]
    public int Table2_id { get; set; }
}

我如何填充Table3的树,该树将引用的Table2和后续的Table1展开为子树?

感谢所有建议

1 个答案:

答案 0 :(得分:0)

可能是这样的吗?

var root = new {TopLevelNodes = Table3.Select(t3=> new {Id = t3.Table2_id, SubLevel = t3.FieldA.Select(t2=>new {t2.FieldA})})};