如何在N级中从数据库填充层次树视图

本文关键字:填充 数据库 层次 视图 | 更新日期: 2023-09-27 18:27:53

我有一个入口类:

   public class Entry
   {
      public int Key { get; set; }
      public string Name { get; set; }
   }

和一个组类别:

   public class Group
   {
      public int Key { get; set; }
      public string Name { get; set; }
      public List<Group> SubGroups { get; set; }
      public List<Entry> Entries { get; set; }
      public List<object> Items
      {
         get
         {
            List<object> childNodes = new List<object>();
            foreach (var group in this.SubGroups)
               childNodes.Add(group);
            foreach (var entry in this.Entries)
               childNodes.Add(entry);
            return childNodes;
         }
      }
   }

我可以手动将数据添加到树中查看如下:

public class TestData
   {
      public List<Group> Groups = new List<Group>();
      public void Load()
      {

         Group grp1 = new Group() { Key = 1, Name = "Group 1", SubGroups = new List<Group>(), Entries = new List<Entry>() };
         Group grp2 = new Group() { Key = 2, Name = "Group 2", SubGroups = new List<Group>(), Entries = new List<Entry>() };
         Group grp3 = new Group() { Key = 3, Name = "Group 3", SubGroups = new List<Group>(), Entries = new List<Entry>() };
         Group grp4 = new Group() { Key = 4, Name = "Group 4", SubGroups = new List<Group>(), Entries = new List<Entry>() };
         //grp1
         grp1.Entries.Add(new Entry() { Key = 1, Name = "Entry number 1" });
         grp1.Entries.Add(new Entry() { Key = 2, Name = "Entry number 2" });
         grp1.Entries.Add(new Entry() { Key = 3, Name = "Entry number 3" });
         //grp2
         grp2.Entries.Add(new Entry() { Key = 4, Name = "Entry number 4" });
         grp2.Entries.Add(new Entry() { Key = 5, Name = "Entry number 5" });
         grp2.Entries.Add(new Entry() { Key = 6, Name = "Entry number 6" });
         //grp3
         grp3.Entries.Add(new Entry() { Key = 7, Name = "Entry number 7" });
         grp3.Entries.Add(new Entry() { Key = 8, Name = "Entry number 8" });
         grp3.Entries.Add(new Entry() { Key = 9, Name = "Entry number 9" });
         //grp4
         grp4.Entries.Add(new Entry() { Key = 10, Name = "Entry number 10" });
         grp4.Entries.Add(new Entry() { Key = 11, Name = "Entry number 11" });
         grp4.Entries.Add(new Entry() { Key = 12, Name = "Entry number 12" });
         Groups.Add(grp1);
         Groups.Add(grp2);
         Groups.Add(grp3);
         Groups.Add(grp4);
         grp1.SubGroups.Add(grp2);
         grp2.SubGroups.Add(grp3);
      }

及其负载数据类

      private void LoadView()
      {
         TestData data = new TestData();
         data.Load();
         GroupView.ItemsSource = data.Groups;
      }

但是,我想从数据库中填充树视图,如下所示:

ID    groupFlag     fatherID       Name
1        1            Null        Group1
2        1             1          Group1.1
3        0             2          Entry1
4        0             2          Entry2
5        1            Null        Group2
6        0             5          Entry3

任何帮助都将不胜感激。。。

如何在N级中从数据库填充层次树视图

根据我从问题中了解到的情况,您将条目和组混合在一个表中,该表还包括嵌套组,为了将数据转换为所需的格式(您在问题中提到的类),我编写了以下代码,该代码使用临时字典,分两步完成:

给定以下输入:

public class FlatItem {
    public int ID;
    public int groupFlag;
    public int? fatherID;
    public string Name;
}
var data = new List<FlatItem>();
data.Add(new FlatItem() { ID = 1, groupFlag = 1, fatherID = null, Name = "Group 1" });
data.Add(new FlatItem() { ID = 2, groupFlag = 1, fatherID = 1, Name = "Group 1.1" });
data.Add(new FlatItem() { ID = 3, groupFlag = 0, fatherID = 2, Name = "Entry 1" });
data.Add(new FlatItem() { ID = 4, groupFlag = 0, fatherID = 2, Name = "Entry 2" });
data.Add(new FlatItem() { ID = 5, groupFlag = 1, fatherID = null, Name = "Group 2" });
data.Add(new FlatItem() { ID = 6, groupFlag = 0, fatherID = 5, Name = "Entry 3" });

转换它:

//temporary dictionary used only while converting
var Groups = new Dictionary<int,Group>();
//final variable where the result will be in
var hierarchy = new List<Group>();
//creating groups
foreach (var item in data.Where(x=> x.groupFlag==1)) {
    var grp = new Group() {
        Key = item.ID,
        Name = item.Name,
        Entries = new List<Entry>(),
        SubGroups = new List<Group>()
    };
    //store reference for later use
    Groups[item.ID] = grp;
    if (item.fatherID == null) {
        //store it in main list
        hierarchy.Add(grp);
    } else {
        //store it in its parent
        Groups[(int)item.fatherID].SubGroups.Add(grp);
    }
}
//fill entries
foreach(var item in data.Where(x=> x.groupFlag == 0)) {
    Groups[(int)item.fatherID].Entries.Add(new Entry() {
        Key = item.ID,
        Name = item.Name
    });
}

现在,变量hierarchy应该包含您想要的内容,并且您可以垃圾收集不再需要的Groups