递归,就是有去有回,自己调用自己。
1 public partial class SiteMaster : MasterPage 2 { 3 public DataSet list = null; 4 5 public List<TreeData> treeDatas = new List<TreeData>(); 6 string str = ""; 7 protected void Page_Load(object sender, EventArgs e) 8 { 9 if (LoginMITBz.currentUser == null) 10 { 11 Response.Redirect("/Login/Login.aspx"); 12 } 13 string sql = "SELECT a.Title,a.TreeId,a.Url,a.ParentID,a.ParentPath FROM dbo.M_Tree a INNER JOIN dbo.M_Permissions b ON a.TreeId = b.TreeId WHERE b.UserId=" + LoginMITBz.currentUser.UserId; 14 list = Hy.DBSqlU.DBUtility_USA.DbHelperSQLDB_USA_MIT_MitInfo.Query(sql); 15 #region Test 16 17 //foreach (DataRow mDr in list.Tables[0].Rows) 18 // { 19 // DataColumn mDc = list.Tables[0].Columns[0]; 20 //Console.WriteLine(mDr["Title"].ToString()); 21 //Console.WriteLine(mDr["Url"].ToString()); 22 23 // //foreach (DataColumn mDc in list.Tables[0].Columns) 24 // //{ 25 // // Console.WriteLine(mDr["Title"].ToString()); 26 // // Console.WriteLine(mDr["Url"].ToString()); 27 // //} 28 //} 29 #endregion 30 31 foreach (DataRow mDr in list.Tables[0].Rows) 32 { 33 DataColumn mDc = list.Tables[0].Columns[0]; 34 TreeData tree = new TreeData(); 35 tree.TreeId = (long)mDr["TreeId"]; 36 tree.Title = mDr["Title"].ToString(); 37 tree.Url = mDr["Url"].ToString(); 38 tree.ParentId = (int)mDr["ParentID"]; 39 treeDatas.Add(tree); 40 } 41 42 43 var data = treeDatas.Where(a => a.ParentId == 0); 44 foreach (var item in data) 45 { 46 str += item.Title + ":" + item.Url + "\r\n"; 47 GetNode((int)item.TreeId); 48 } 49 } 50 51 public void GetNode(int parentId) 52 { 53 var data = treeDatas.Where(a => a.ParentId == parentId); 54 foreach (var item in data) 55 { 56 str += item.Title + ":" + item.Url + "\r\n"; 57 GetNode((int)item.TreeId); 58 } 59 } 60 } 61 62 63 64 public class TreeData 65 { 66 public long? TreeId { get; set; } 67 public int? ParentId { get; set; } 68 public string Title { get; set; } 69 public string Url { get; set; } 70 }
原文地址:https://www.cnblogs.com/gyl5201314/p/9154514.html
时间: 2024-11-06 03:39:00