1.首先定义树形结构类
/// <summary> /// 构建树形结构类 /// </summary> public class TreeModel { public string ID { set; get; } public string PARENTID { set; get; } public string NAME { set; get; } public List<TreeModel> TREECHILDREN { set; get; } }
2.利用反射将泛型list转换为树形结构类
/// <summary> /// 公用递归(反射转换List) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="allList">数据列表</param> /// <param name="parentId">父级ID</param> /// <param name="idField">id字段名称</param> /// <param name="parentIdField">父级id字段名称</param> /// <param name="nameField">name字段名称</param> /// <returns></returns> public List<TreeModel> ConversionList<T>(List<T> allList, string parentId, string idField, string parentIdField, string nameField) { List<TreeModel> list = new List<TreeModel>(); TreeModel model = null; foreach (var item in allList) { model = new TreeModel(); foreach (System.Reflection.PropertyInfo p in item.GetType().GetProperties()) { if (p.Name == idField) { model.ID = p.GetValue(item).ToString(); } if (p.Name == parentIdField) { model.PARENTID = p.GetValue(item).ToString(); } if (p.Name == nameField) { model.NAME = p.GetValue(item).ToString(); } } list.Add(model); } return OperationParentData(list, parentId); }
3.递归方法
/// <summary> /// 公用递归(处理递归最父级数据) /// </summary> /// <param name="treeDataList">树形列表数据</param> /// <param name="parentId">父级Id</param> /// <returns></returns> public List<TreeModel> OperationParentData(List<TreeModel> treeDataList, string parentId) { var data = treeDataList.Where(x => x.PARENTID == parentId); List<TreeModel> list = new List<TreeModel>(); foreach (var item in data) { OperationChildData(treeDataList, item); list.Add(item); } return list; } /// <summary> /// 公用递归(递归子级数据) /// </summary> /// <param name="treeDataList">树形列表数据</param> /// <param name="parentItem">父级model</param> public void OperationChildData(List<TreeModel> treeDataList, TreeModel parentItem) { var subItems = treeDataList.Where(ee => ee.PARENTID == parentItem.ID).ToList(); if (subItems.Count != 0) { parentItem.TREECHILDREN = new List<TreeModel>(); parentItem.TREECHILDREN.AddRange(subItems); foreach (var subItem in subItems) { OperationChildData(treeDataList, subItem); } } }
4.调用
List<TreeModel> treeViewModels = new List<TreeModel>();
//list为待处理数据集,pID为父级节点ID;然后分别传入待处理数据集中ID的字段名称,父级ID的字段名称和需要展示Name字段名称
treeViewModels = ConversionList(list, pID, "ID", "PID", "EFName");
5.处理后数据结果
[{ "ID": "00000000-0000-0000-0000-000000000001", "PARENTID": "00000000-0000-0000-0000-000000000000", "NAME": "1级", "TREECHILDREN": [{ "ID": "00000000-0000-0000-0000-000000000002", "PARENTID": "00000000-0000-0000-0000-000000000001", "NAME": "2级", "TREECHILDREN": [{ "ID": "00000000-0000-0000-0000-000000000003", "PARENTID": "00000000-0000-0000-0000-000000000002", "NAME": "3级", "TREECHILDREN": [{ "ID": "00000000-0000-0000-0000-000000000005", "PARENTID": "00000000-0000-0000-0000-000000000003", "NAME": "4级", "TREECHILDREN": null }] }] }] }, { "ID": "00000000-0000-0000-0000-000000000004", "PARENTID": "00000000-0000-0000-0000-000000000000", "NAME": "1级", "TREECHILDREN": null } ]