c# -无限递归获取分类,传统for无限递归和 linq无限递归获取分类
【*重点】 核心业务代码
public string CommonClassList(){ List list =getList(“获取信息列表”); PrimevalDataList.Clear(); //清空缓存 ReturnDataList.Clear();//清空缓存 PrimevalDataList = list;//初始化数据 Recursive(0); //处理数据 return ReturnDataList; //输出处理后的数据}#region 递归渲染列表/// /// 原数据/// private static List PrimevalDataList = new List();/// /// 递归后的结果数据/// private static List ReturnDataList = new List();/// /// 【递归深度】 /// private static int RecursiveIndex = 0;/// /// 递归循环/// /// 父级ID/// 间隔符/// 标识符/// 间隔符是否在前private static void Recursive(int BaseID = 0, string intervalSign = " ", string markSign = " └─", bool intervalSignIsTop = true){ RecursiveIndex = BaseID == 0 ? 0 : ++RecursiveIndex; //符号初始化 string _intervalSign = BaseID == 0 ? "" : intervalSign; string _markSign = BaseID == 0 ? "" : markSign; foreach (var item in PrimevalDataList) { if (item.ParentId == BaseID) { CommonClassBusinessModel mode = new CommonClassBusinessModel() { ClassId = item.ClassId, CommonName = item.CommonName, ParentId = item.ParentId, DisplayOrder = item.DisplayOrder, Layer = item.Layer, Type = item.Type, Status = item.Status, ParentName = item.ParentName }; string _intervalSign2 = _intervalSign; for (int i = 0; i < RecursiveIndex; i++) { _intervalSign2 += (_intervalSign + _intervalSign + _intervalSign); } if (intervalSignIsTop) { mode.CommonName2 = $@"{_intervalSign2}{_markSign}{item.CommonName}"; } else { mode.CommonName2 = $@"{_markSign}{_intervalSign2}{item.CommonName}"; } ReturnDataList.Add(mode); int number = RecursiveIndex; Recursive(item.ClassId, intervalSign, markSign, intervalSignIsTop); RecursiveIndex = number; } }}#endregion
public class CommonClassModel{ public int ClassId { get; set; } public string CommonName { get; set; } //扩展字段 父ID public int ParentId { get; set; } public int DisplayOrder { get; set; } public int Layer { get; set; } public int Type { get; set; } public int Status { get; set; } //扩展字段 父名称 public string ParentName { get; set; }}public class CommonClassBusinessModel: CommonClassModel{ //存放递归后的名称 public string CommonName2 { get; set; }}
一、传统for无限递归获取分类
代码:
运行结果:
二、linq无限递归获取分类
代码:
运行结果:
三、无限分类列表-实战代码
效果
封装的递归代码
#region 递归渲染列表 /// /// 原数据 /// private static List PrimevalDataList = null; /// /// 递归后的结果数据 /// private static List ReturnDataList = new List(); /// /// 【递归深度】 /// private static int RecursiveIndex = 0; private static void Recursive(int BaseID = 0) { RecursiveIndex = BaseID == 0 ?0: ++RecursiveIndex; //符号 string sign = BaseID==0? "": " "; string BaseSign = BaseID == 0 ? "" : " └─"; foreach (Sys_Menu item in PrimevalDataList) { if (item.BaseID == BaseID) { SysMenuBusinessModel mode = new SysMenuBusinessModel() { Sys_MenuID= item.Sys_MenuID, Name = item.Name, Sort = item.Sort, BaseID = item.BaseID, LinkUrl = item.LinkUrl, IsLock = item.IsLock, Remark = item.Remark, IsSys = item.IsSys, CreateUserID = item.CreateUserID, CreateUserName = item.CreateUserName, CreateDateTime = item.CreateDateTime, UpdateUserID = item.UpdateUserID, UpdateUserName = item.UpdateUserName, UpdateDateTime = item.UpdateDateTime, }; for (int i = 0; i < RecursiveIndex; i++) { sign += sign; } mode.BusinessName = $@"{sign}{BaseSign}{item.Name}"; ReturnDataList.Add(mode); int number = RecursiveIndex;//存储当前递归深度 Recursive(item.Sys_MenuID); RecursiveIndex = number; } } } #endregion
调用
public JsonResult JsonList(Sys_Menu model) { JsonResultModel resultModel = new JsonResultModel(); PrimevalDataList = bll.List(); //获取原始数据 ReturnDataList.Clear(); //清空结果数据 Recursive(0);//递归数据 resultModel.count = ReturnDataList.Count;//获取结果数据的数量 resultModel.code = 0; resultModel.msg = "成功"; resultModel.data = ReturnDataList;//赋值结果数据 return Json(resultModel, JsonRequestBehavior.AllowGet); }
结果模型类
/// /// JSON结果模型类 /// /// /// public class JsonResultModel { /// ///数据状态一切正常的状态码 0:成功,1:失败 /// public int code { get; set; } = 1; /// /// 状态信息 /// public String msg { get; set; } = "失败"; /// /// 数据详情 /// public List data { get; set; } /// /// 数据总条数 /// public int count { get; set; } = 0; }
原始数据模型类 Sys_Menu
/// /// 功能菜单Sys_Menu /// public class Sys_Menu { /// /// 功能菜单ID /// public int Sys_MenuID { get; set; } /// /// 菜单名称 /// public string Name { get; set; } /// /// 排序 /// public int Sort { get; set; } /// /// 父ID /// public int BaseID { get; set; } /// /// 链接地址 /// public string LinkUrl { get; set; } /// /// 是否隐藏0显示1隐藏 /// public bool IsLock { get; set; } /// /// 备注说明 /// public string Remark { get; set; } /// /// 系统默认 /// public bool IsSys { get; set; } /// /// 创建人ID /// public string CreateUserID { get; set; } /// /// 创建人姓名 /// public string CreateUserName { get; set; } /// /// 创建时间 /// public DateTime CreateDateTime { get; set; } /// /// 修改人ID /// public string UpdateUserID { get; set; } /// /// 修改人 /// public string UpdateUserName { get; set; } /// /// 修改时间 /// public DateTime UpdateDateTime { get; set; } /// /// 是否删除 /// public bool IsDelete { get; set; } }
结果数据模型类
/// /// 菜单业务模型类 /// public class SysMenuBusinessModel:Sys_Menu { public string BusinessName { set; get; } //业务名称,递归封装后的名称 }
四、根据第三示例实现完美封装
递归渲染列表
#region 递归渲染列表 /// /// 原数据 /// private static List PrimevalDataList = null; /// /// 递归后的结果数据 /// private static List ReturnDataList = new List(); /// /// 【递归深度】 /// private static int RecursiveIndex = 0; /// /// 递归循环 /// /// 父级ID /// 间隔符 /// 标识符 /// 间隔符是否在前 private static void Recursive(int BaseID = 0, string intervalSign = " ", string markSign = " └─", bool intervalSignIsTop = true) { RecursiveIndex = BaseID == 0 ? 0 : ++RecursiveIndex; //符号初始化 string _intervalSign = BaseID == 0 ? "" : intervalSign; string _markSign = BaseID == 0 ? "" : markSign; foreach (Sys_Menu item in PrimevalDataList) { if (item.BaseID == BaseID) { SysMenuBusinessModel mode = new SysMenuBusinessModel() { Sys_MenuID = item.Sys_MenuID, Name = item.Name, Sort = item.Sort, BaseID = item.BaseID, LinkUrl = item.LinkUrl, IsLock = item.IsLock, Remark = item.Remark, IsSys = item.IsSys, CreateUserID = item.CreateUserID, CreateUserName = item.CreateUserName, CreateDateTime = item.CreateDateTime, UpdateUserID = item.UpdateUserID, UpdateUserName = item.UpdateUserName, UpdateDateTime = item.UpdateDateTime, }; for (int i = 0; i < RecursiveIndex; i++) { _intervalSign += _intervalSign; } if (intervalSignIsTop) { mode.BusinessName = $@"{_intervalSign}{_markSign}{item.Name}"; } else { mode.BusinessName = $@"{_markSign}{_intervalSign}{item.Name}"; } ReturnDataList.Add(mode); int number = RecursiveIndex; Recursive(item.Sys_MenuID, intervalSign, markSign, intervalSignIsTop); RecursiveIndex = number; } } } #endregion
第一次调用时
PrimevalDataList = bll.List();//从数据库里读取出数据赋值给“原始数据变量”ReturnDataList.Clear();//把“递归后的结果数据”全部移除Recursive(0);//执行递归方法resultModel.data = ReturnDataList;//把“递归后的结果数据”赋值给返回的对象变量
再次调用时
//重新渲染递归符ReturnDataList.Clear();//把递归后的全部数据移除Recursive(BaseID: 0, intervalSign: "─", markSign: "└", intervalSignIsTop: false);//重新调用递归,从父级id为“0”开始递归,并且把“间隔符”和“标识符”重新定义,另外把间隔符后置ViewBag.List = ReturnDataList;//把递归渲染好的数据赋值给返回的对象变量
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~