递归算法,将叶子节点存入一个空的stringbuilder变量
1 private void GetLeafIDSb(StringBuilder result, DataTable dt, string childColumnName, string parentColumnName, int Id) 2 { 3 DataRow[] temp1 = dt.Select(parentColumnName + "= " + Id + " "); 4 if (temp1.Length == 0) 5 { 6 result.Append(Id + ","); 7 } 8 else 9 { 10 foreach (DataRow item2 in temp1) 11 { 12 DataRow[] temp2 = dt.Select(parentColumnName + "=" + item2[childColumnName].ToString() + " "); 13 GetLeafIDSb(result, dt, childColumnName, parentColumnName, int.Parse(item2[childColumnName].ToString())); 14 } 15 } 16 }
将上方的方法进行包装,返回叶子节点的字符串
1 private string GetLeafIdStr(DataTable dt, string childColumnName, string parentColumnName, int Id) 2 { 3 StringBuilder sb = new StringBuilder(); 4 GetLeafIDSb(sb, dt, childColumnName, parentColumnName, Id); 5 sb = sb.Remove(sb.Length - 1, 1); 6 return sb.ToString(); 7 }
使用时直接调用下面这个方法即可,会返回指定节点的叶子节点字符串
时间: 2024-11-06 13:17:04