码迷,mamicode.com
首页 > 其他好文 > 详细

树形结构根据某节点查询本节点及下属所有子节点的递归实现

时间:2018-06-25 16:59:13      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:each   any   where   查找   mode   ict   class   sqlhelper   rom   

数据表中CompanyId,ParentCompany,有层级关系,树形结构根据某节点查询本节点及下属所有子节点的递归实现如下:

        public string ids = "";

        /// <summary>
        /// 根据CompanyId 查找到子单位id
        /// </summary>
        /// <param name="companyId"></param>
        /// <returns></returns>
        private string GetCompanysIds(string CompanyId)
        {
            StringBuilder strSqlcode = new StringBuilder();
            strSqlcode.Append(" select * from SystemCompany where CompanyId = @CompanyId ");
            SqlHelper<SystemCompany> sqlHelpercode = new SqlHelper<SystemCompany>();
            Dictionary<string, object> parameterscode = new Dictionary<string, object>();
            parameterscode.Add("@CompanyId", CompanyId);
            SystemCompany systemcompany = sqlHelpercode.FindModel(strSqlcode.ToString(), parameterscode);
            string s = systemcompany.CompanyId;
            string CompanysIds = GetsIds(s, systemcompany.CompanyId);
            return CompanysIds;
        }

        private string GetsIds(string s, string CompanyId)
        {
            List<string> companyids = new List<string>();
            StringBuilder strSqlcode = new StringBuilder();
            strSqlcode.Append(" select * from SystemCompany where ParentCompany = @ParentCompany ");
            SqlHelper<SystemCompany> sqlHelpercode = new SqlHelper<SystemCompany>();
            Dictionary<string, object> parameterscode = new Dictionary<string, object>();
            parameterscode.Add("@ParentCompany", CompanyId);
            List<SystemCompany> systemcompany = sqlHelpercode.Find(strSqlcode.ToString(), parameterscode);
            if (systemcompany.Count > 0)
            {
                foreach (var item in systemcompany)
                {
                    s = s + ";" + item.CompanyId;
                    ids = s;
                    GetsIds(s, item.CompanyId);
                }
            }
            else
            {
                ids = s;
            }

            return ids;
        }

 

向上查找同理

树形结构根据某节点查询本节点及下属所有子节点的递归实现

标签:each   any   where   查找   mode   ict   class   sqlhelper   rom   

原文地址:https://www.cnblogs.com/yangwujun/p/9224377.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!