根据路径字符串的集合创建层次结构数据表,并将唯一ID分配给父节点和子节点

时间:2018-11-02 14:06:58

标签: c# .net collections datatable

我在数据库中有一个平面数据条目,每一行都有一个路径列表。需要分解并创建数据表的示例,需要生成唯一的ID:

-Parent1
- - Child1 
- - - ChildA of Child1
- - - ChildB of Child1
- - Child2 
- - - ChildA of Child2
- - - ChildB of Child2
- Parent2

数据行的条目类似于:

\Parent1\child1\childA\
\Parent1\child1\childB\
\Parent1\child2\childA\
\Parent1\child2\childC\
\Parent2\child3\child4\childE
\Parent2\child4\child1\child6\child7

1 个答案:

答案 0 :(得分:0)

尝试如下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication24
{
    public partial class Form1 : Form
    {
        public DataTable dt = null;
        public int ID = 0;
        public Form1()
        {
            InitializeComponent();

            List<string> input = new List<string>() {
               @"\Parent1\child1\childA\",
               @"\Parent1\child1\childB\",
               @"\Parent1\child2\childA\",
               @"\Parent1\child2\childC\",
               @"\Parent2\child3\child4\childE",
               @"\Parent2\child4\child1\child6\child7"
            };

            List<List<string>> splitInputs = input.Select(x => x.Split(new char[] { '\\' }, StringSplitOptions.RemoveEmptyEntries).ToList()).ToList();
            TreeNode root = null;
            treeView1.Nodes.Clear();
            RecursiveAdd(root, splitInputs);
            treeView1.ExpandAll();

            CreateDataTable(splitInputs);

        }
        public void RecursiveAdd(TreeNode parent, List<List<string>> paths)
        {
            var folders = paths.GroupBy(x => x[0]).ToList();
            foreach (var folder in folders)
            {
                TreeNode node = new TreeNode(folder.Key);
                if (parent == null)
                {
                    treeView1.Nodes.Add(node);
                }
                else
                {
                    parent.Nodes.Add(node);
                }
                List<List<string>> childNodes = folder.Select(x => x.Skip(1).ToList()).ToList();
                if (childNodes.Count > 1)
                {
                    RecursiveAdd(node, childNodes);
                }
            }

        }
        public void CreateDataTable(List<List<string>> paths)
        {
            dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Parent ID", typeof(int));

            int rootID = ID++;
            CreateDataTableRecursive(rootID, paths);
        }
        public void CreateDataTableRecursive(int parentID, List<List<string>> paths)
        {
            var folders = paths.GroupBy(x => x[0]).ToList();
            foreach (var folder in folders)
            {
                DataRow row = dt.Rows.Add();

                row["ID"] = ID;
                row["Name"] = folder.Key;
                int newID = ID++;
                row["Parent ID"] = parentID;
                List<List<string>> childNodes = folder.Select(x => x.Skip(1).ToList()).ToList();
                if (childNodes.Count > 1)
                {
                    CreateDataTableRecursive(newID, childNodes);
                }
            }

        }

    }
}
相关问题