1 /// <summary> 2 /// 文件夹实体 3 /// </summary> 4 public class Folder 5 { 6 /// <summary> 7 /// 文件夹ID 8 /// </summary> 9 public int id { get; set; } 10 /// <summary> 11 /// 文件夹名称 12 /// </summary> 13 public string name { get; set; } 14 /// <summary> 15 /// 父级ID 16 /// </summary> 17 public int folder_parent_id { get; set; } 18 }
1 /// <summary> 2 /// 重构文件夹层级列表 3 /// </summary> 4 /// <param name="listSource">源数据</param> 5 /// <param name="pId">父级ID</param> 6 /// <param name="count">添加元素的次数</param> 7 /// <param name="levelNum">当前层级</param> 8 /// <param name="listResult">返回结果集</param> 9 private void RefactorFolderLevelList(List<Folder> listSource, int pId, int count, List<int> levelNum, List<Folder> listResult) 10 { 11 var len_source = listSource.Count(); 12 if (count < len_source) 13 { 14 var tmp_list = listSource.Where(m => m.folder_parent_id == pId); 15 var len_tmp_list = tmp_list.Count(); 16 bool addFlag = true; 17 int level = levelNum.Count(); 18 int i = 0; 19 foreach (var item in tmp_list) 20 { 21 i++; 22 item.name = FillString(level, "——") + item.name; 23 var model = listResult.Where(m => m.id == item.id).FirstOrDefault(); 24 if (pId == 0 && model != null) 25 continue; 26 else 27 listResult.Add(item); 28 ++count; 29 if (listSource.Where(m => m.folder_parent_id == item.id).Count() > 0) 30 { 31 if (!levelNum.Contains(item.id) && addFlag) 32 { 33 addFlag = false; 34 levelNum.Add(item.id); 35 } 36 RefactorFolderLevelList(listSource, item.id, count, levelNum, listResult); 37 } 38 else 39 { 40 if (i < len_tmp_list) 41 continue; 42 levelNum = new List<int>(); 43 RefactorFolderLevelList(listSource, 0, count, levelNum, listResult); 44 } 45 } 46 } 47 } 48 /// <summary> 49 /// 字符串填充 50 /// </summary> 51 /// <param name="n">填充的次数</param> 52 /// <param name="str">需要填充的字符串</param> 53 /// <returns></returns> 54 private string FillString(int n, string str) 55 { 56 StringBuilder ret = new StringBuilder(); 57 for (int i = 0; i < n; i++) 58 { 59 ret.Append(str); 60 } 61 return ret.ToString(); 62 }
效果图:
时间: 2024-11-08 02:13:43