csharp: DataRelation objects to represent a parent/child/Level relationship
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load( object sender, EventArgs e) { var sections = new List<Section> { new Section { Id = 1, Name = "中国" , ParentID = 0 }, new Section { Id = 2, Name = "江西" , ParentID = 1 }, new Section { Id = 3, Name = "江苏" , ParentID = 1 }, new Section { Id = 4, Name = "南京" , ParentID = 3 }, new Section { Id = 5, Name = "南昌" , ParentID = 2 }, new Section { Id = 6, Name = "东湖区" , ParentID = 5 }, new Section { Id = 7, Name = "广东" , ParentID = 1 }, new Section { Id = 8, Name = "深圳" , ParentID = 7 }, new Section { Id = 9, Name = "罗湖区涂聚文" , ParentID = 8 } }; //sections = sections.OrderBy(x => x.ParentID).ThenBy(x => x.Name).ToList(); //var stack = new Stack<Section>(); //// Grab all the items without parents //foreach (var section in sections.Where(x => x.ParentID == default(int)).Reverse()) //{ // stack.Push(section); // sections.RemoveAt(0); //} var output = new List<Section>(); //while (stack.Any()) //{ // var currentSection = stack.Pop(); // var children = sections.Where(x => x.ParentID == currentSection.Id).Reverse(); // foreach (var section in children) // { // stack.Push(section); // sections.Remove(section); // } // output.Add(currentSection); //} //sections = output; List<MySection> mys = MenuHelper.GetMyMenuCollection(sections); //ResolveDDL<MySectionMenu>(mys); var outputlist = ( from mysection in mys orderby mysection.TreeLevel descending select mysection).ToList(); var outputstringlist = ( from mysection in mys orderby mysection.TreeLevel descending select mysection.Name).ToList(); for ( int i = 0; i < outputlist.Count; i++) { //Response.Write(string.Format("ID:{0} ParentID: {1} TreeLevel: {2} Name:{3}<br/>", mys[i].Id, mys[i].ParentID, mys[i].TreeLevel,mys[i].Name)); Response.Write( string .Format( "ID:{0} ParentID: {1} TreeLevel: {2} Name:{3}<br/>" , outputlist[i].Id, outputlist[i].ParentID, outputlist[i].TreeLevel, outputlist[i].Name)); } } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mys"></param> protected void ResolveDDL<T>(List<T> mys) where T : MyBaseSection, new () { ResolveDDL<T>(mys, -1, true ); } /// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mys"></param> /// <param name="currentId"></param> protected void ResolveDDL<T>(List<T> mys, int currentId) where T : MyBaseSection, new () { ResolveDDL<T>(mys, currentId, true ); } /// <summary> /// 将一个树型结构放在一个下列列表中可供选择 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="currentId"></param> /// <param name="mys"></param> protected void ResolveDDL<T>(List<T> mys, int currentId, bool addRootNode) where T : MyBaseSection, new () { if (addRootNode) { // 所有节点的TreeLevel加一,然后添加根节点 foreach (T my in mys) { my.TreeLevel += 1; } T root = new T(); root.Name = "--根节点--" ; root.Id = 0; root.TreeLevel = 0; mys.Insert(0, root); } // currentId==-1表示当前节点不存在 if (currentId != -1) { // 本节点不可点击(也就是说当前节点不可能是当前节点的父节点) // 并且本节点的所有子节点也不可点击,你想如果当前节点跑到子节点的子节点,那么这些子节点就从树上消失了 bool startChileNode = false ; int startTreeLevel = 0; foreach (T my in mys) { if (my.Id == currentId) { startTreeLevel = my.TreeLevel; my.Enabled = false ; startChileNode = true ; } else { if (startChileNode) { if (my.TreeLevel > startTreeLevel) { my.Enabled = false ; } else { startChileNode = false ; } } } } } } } /// <summary> /// / /// </summary> public class Section { public int Id { get ; set ; } public string Name { get ; set ; } public int ParentID { get ; set ; } } /// <summary> /// /// </summary> public class MySection { public int Id { get ; set ; } public string Name { get ; set ; } public int ParentID { get ; set ; } public int TreeLevel { get ; set ; } } /// <summary> /// /// </summary> public class MySectionMenu : MyBaseSection { } /// <summary> /// /// </summary> public class MyBaseSection { public int Id { get ; set ; } public int ParentId { get ; set ; } public string Name { get ; set ; } /// <summary> /// 本菜单在树形结构中层级(从0开始) /// </summary> public int TreeLevel { get ; set ; } /// <summary> /// 是否可用(默认true) /// </summary> public bool Enabled { get ; set ; } /// <summary> /// 是否叶子节点(默认false) /// </summary> public bool IsTreeLeaf { get ; set ; } } /// <summary> /// /// </summary> public class MenuHelper { /// <summary> /// /// </summary> /// <param name="oldMenus"></param> /// <returns></returns> public static List<MySection> GetMyMenuCollection(List<Section> oldMenus) { List<MySection> newMenus = new List<MySection>(); ResolveMenuCollection(oldMenus, newMenus, 0, 0); return newMenus; } /// <summary> /// /// </summary> /// <param name="oldMenus"></param> /// <param name="newMenus"></param> /// <param name="parentId"></param> /// <param name="level"></param> /// <returns></returns> private static int ResolveMenuCollection(List<Section> oldMenus, List<MySection> newMenus, int parentId, int level) { int count = 0; foreach (Section menu in oldMenus) { if (menu.ParentID == parentId) { count++; MySection my = new MySection(); newMenus.Add(my); my.TreeLevel = level; my.Id = menu.Id; my.Name = menu.Name; my.ParentID = menu.ParentID; level++; int childCount = ResolveMenuCollection(oldMenus, newMenus, menu.Id, level); level--; } } return count; } } |
显示结果:
ID:1 ParentID: 0 TreeLevel: 0 Name:中国
ID:2 ParentID: 1 TreeLevel: 1 Name:江西
ID:5 ParentID: 2 TreeLevel: 2 Name:南昌
ID:6 ParentID: 5 TreeLevel: 3 Name:东湖区
ID:3 ParentID: 1 TreeLevel: 1 Name:江苏
ID:4 ParentID: 3 TreeLevel: 2 Name:南京
ID:7 ParentID: 1 TreeLevel: 1 Name:广东
ID:8 ParentID: 7 TreeLevel: 2 Name:深圳
ID:9 ParentID: 8 TreeLevel: 3 Name:涂聚文
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2013-05-24 Csharp: 請假輸入兩個日期:(只考慮最多相差一個月)对于不同月份的数据考虑月份数据分界