【问题标题】:Populating a tree recursively with database contents使用数据库内容递归地填充树
【发布时间】:2012-05-16 03:34:25
【问题描述】:

我正在使用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 展开到子树中?

感谢所有建议

【问题讨论】:

  • 刚刚意识到 C# 没有我认为理所当然的内置数据结构。幸运的是 C5 添加了这么多必需的功能集。

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


【解决方案1】:

可能是这样的?

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

【讨论】:

  • 嗯,大概就是这样,谢谢。虽然你能告诉我它是什么吗? - 例如:它是嵌套列表还是实际树?
  • 是的,linq 加上匿名类,我看不到你要完成的全貌,但你可能根本不需要它,你的类已经与你想要的方式相关。
猜你喜欢
  • 2015-06-29
  • 2011-07-17
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 1970-01-01
  • 1970-01-01
  • 2013-01-11
  • 2017-02-02
相关资源
最近更新 更多