C# List的树状结构数据-递归使用

14 篇文章 0 订阅
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EasyUITree
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        List<tree> listTreeAll = new List<tree>();

        tree treeAll = new tree();

        public tree BindNew(tree node)
        {
            DataTable dr = GetReader(node.id);
            tree n = new tree();
            for (int i = 0; i < dr.Rows.Count; i++)
            {
                if (Convert.ToInt32(dr.Rows[i]["pid"]) == 0)
                {

                    n.id = Convert.ToInt32(dr.Rows[i]["id"]);
                    n.text = dr.Rows[i]["text"].ToString();
                    n.pid = Convert.ToInt32(dr.Rows[i]["pid"]);
                    n.child = GetChild(n);

                }
            }
            return n;
        }
        public List<tree> GetChild(tree node)
        {
            DataTable dr = GetReader(node.id);
            List<tree> child = new List<tree>();
            for (int i = 0; i < dr.Rows.Count; i++)
            {

                tree n = new tree();
                n.id = Convert.ToInt32(dr.Rows[i]["id"]);
                n.text = dr.Rows[i]["text"].ToString();
                n.pid = Convert.ToInt32(dr.Rows[i]["pid"]);
                child.Add(n);
                DataTable dr1 = GetReader(n.id);
                if (dr1 != null)
                {
                    n.child = GetChild(n);
                }


            }
            return child;
        }

        





        /// <summary>
        /// 测试
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            tree model = new tree();
            model.id = 0;
            BindNew(model);//递归

            List<tree> list = new List<tree>();
            list.Add(BindNew(model));




        }

        public DataTable GetReader(int pid)
        {
            string sql = " select * from t_tree where pid = " + pid + "  ";  //where pid = " + pid + "  ";
            string ConnectionString = "uid=sa;pwd=qazwsx;initial catalog=TestDBase;data source=DESKTOP-HKIRA54;Connect Timeout=900";
            using (SqlConnection con = new SqlConnection(ConnectionString))
            {
                SqlCommand cmd = new SqlCommand(sql, con);
                con.Open();
                DataSet ds = new DataSet();
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = cmd;
                adapter.Fill(ds);

                DataTable table = ds.Tables[0];
                return table;
            }
        }
    }


    public class tree
    {
        public int id { get; set; }
        public string text { get; set; }
        public int pid { get; set; }
        public List<tree> child { get; set; }


    }


原文:https://www.cnblogs.com/dullbaby/p/5043675.html


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用递归来查询树状结构数据。假设你有一个包含父子关系的表,其中每个记录包含一个唯一的ID和一个指向父记录ID的字段。下面是一个示例的PHP代码,用于递归查询树状结构数据: ```php function getChildren($data, $parentId) { $result = array(); foreach ($data as $row) { if ($row['parent_id'] == $parentId) { $children = getChildren($data, $row['id']); if ($children) { $row['children'] = $children; } $result[] = $row; } } return $result; } // 假设你有一个包含树状结构数据的数组 $data = array( array('id' => 1, 'name' => 'Node 1', 'parent_id' => 0), array('id' => 2, 'name' => 'Node 2', 'parent_id' => 0), array('id' => 3, 'name' => 'Node 3', 'parent_id' => 1), array('id' => 4, 'name' => 'Node 4', 'parent_id' => 1), array('id' => 5, 'name' => 'Node 5', 'parent_id' => 2), array('id' => 6, 'name' => 'Node 6', 'parent_id' => 4), ); $tree = getChildren($data, 0); // 打印树状结构数据 echo json_encode($tree); ``` 上述代码中,`getChildren` 函数接收一个数据数组和一个父记录ID作为参数。它遍历数据数组,找到所有具有指定父记录ID的记录,并递归调用 `getChildren` 函数来获取子记录。如果子记录存在,则将其添加到父记录的 `children` 字段中,最后返回结果数组。 在示例中,我们假设根记录的 `parent_id` 为0。你可以根据具体的数据结构进行调整。最后,我们使用 `json_encode` 函数将结果以JSON格式输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值