数据表中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; }
向上查找同理
原文地址:https://www.cnblogs.com/yangwujun/p/9224377.html
时间: 2024-11-05 18:46:36