精华内容
下载资源
问答
  • c# .net无限递归获取分类,传统for无限递归和 linq无限递归获取分类

    【*重点】 核心业务代码

    
    
    
    
    public string CommonClassList()
    { 
    	List<CommonClassModel> list =getList(“获取信息列表”);
    	PrimevalDataList.Clear(); //清空缓存
    	ReturnDataList.Clear();//清空缓存
    	PrimevalDataList = list;//初始化数据
    	Recursive(0); //处理数据
    	return ReturnDataList; //输出处理后的数据
    }
    
    #region 递归渲染列表
    /// <summary>
    /// 原数据
    /// </summary>
    private static List<CommonClassModel> PrimevalDataList = new List<CommonClassModel>();
    /// <summary>
    /// 递归后的结果数据
    /// </summary>
    private static List<CommonClassBusinessModel> ReturnDataList = new List<CommonClassBusinessModel>();
    
    /// <summary>
    /// 【递归深度】 
    /// </summary>
    private static int RecursiveIndex = 0;
    
    /// <summary>
    /// 递归循环
    /// </summary>
    /// <param name="_BaseID">父级ID</param>
    /// <param name="intervalSign">间隔符</param>
    /// <param name="markSign">标识符</param>
    /// <param name="intervalSignIsTop">间隔符是否在前</param>
    private static void Recursive(int BaseID = 0, string intervalSign = "&nbsp;&nbsp;", string markSign = "<span style=\"color:#cccccc;\">&nbsp;&nbsp;&nbsp;&nbsp;└─</span>", 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无限递归获取分类

    代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DiGui
    {
        class Program
        { 
            /// <summary>
            /// 缓存数据
            /// </summary>
            private static List<TypeModel> typeList = null;
            /// <summary>
            /// 递归后的数据
            /// </summary>
            static List<TypeModel> RecursiveTypeList = new List<TypeModel>();
    
    
            /// <summary>
            /// 【递归深度】
            /// 如果是2级分类
            /// 一级分类是“手机”
            /// 二级分类是“小米手机”
            /// 递归第一次是“手机”深度是0
            /// 递归第二次是“小米手机”深度是1
            /// 以此类推
            /// </summary>
            static int RecursiveIndex = 0;
            static void Main(string[] args)
            {
                RecursiveEXE();
                Console.Read();
            }
    
            #region 递归给分类重新排序
            /// <summary>
            /// 递归给分类重新排序
            /// </summary>
            private static void RecursiveEXE()
            {
                typeList = GetList();
    
                string y = "";
                foreach (var type in typeList)
                {
                    y = y + $"{type.ID}+{ type.Name} \n"; 
                }
                Console.WriteLine("源数据:");
                Console.WriteLine(y);
    
    
                Recursive(0);
                string s = "";
                foreach (var type in RecursiveTypeList)
                {
                    s = s + type.sign+ type.Name + "\n";
                } 
                Console.WriteLine("【递归】后的数据:");
                Console.WriteLine($"{s}");
            }
    
    
            /// <summary>
            /// 递归方法
            /// </summary>
            /// <param name="id"></param> 
            private static void Recursive(int id)
            {  
                int a = 0; 
                for (int i = 0; i < typeList.Count; i++)
                {
                    TypeModel typeModel = typeList[i];
                    if (typeModel.BaseID == id)
                    {
                        a = a+1;
                        TypeModel Type = new TypeModel();
                        Type.ID = typeModel.ID;
                        Type.Name = typeModel.Name;
                        Type.BaseID = typeModel.BaseID;
    
                        if (a == 1 && typeModel.BaseID != 0)
                        {
                           
                            RecursiveIndex = RecursiveIndex + 1;
                        }
                        string sign = "";
                        for (int j = 0; j < RecursiveIndex; j++)
                        {
                            sign = sign + "-";
                        }
    
                        if (typeModel.BaseID == 0)
                        {
                            RecursiveIndex = 0;
                            sign = "";
                        }
    
                        Type.sign = sign;
                        RecursiveTypeList.Add(Type);
    
                        //这里需要声明一个临时变量存储当前递归深度,让这个变量去等待递归;否则,将会覆盖递归深度,造成符号数据错误,也就是 sign变量
                        int temporary = RecursiveIndex;
                        Recursive(typeModel.ID);
                        //当前递归结束后把临时存储的递归深度,赋值给当前深度
                        RecursiveIndex = temporary;
                     
                    }  
                }    
            }
    
            private static List<TypeModel> GetList()
            {
                List<TypeModel> typeList = new List<TypeModel>();
    
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 1,
                        Name = "手机",
                        BaseID = 0
                    };
                    typeList.Add(typeModel);
                }
    
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 2,
                        Name = "电脑",
                        BaseID = 0
                    };
                    typeList.Add(typeModel);
                }
    
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 3,
                        Name = "小米手机",
                        BaseID = 1
                    };
                    typeList.Add(typeModel);
                }
    
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 4,
                        Name = "小米电脑",
                        BaseID = 2
                    };
                    typeList.Add(typeModel);
                }
    
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 5,
                        Name = "华为手机",
                        BaseID = 1
                    };
                    typeList.Add(typeModel);
                }
    
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 6,
                        Name = "三星手机",
                        BaseID = 1
                    };
                    typeList.Add(typeModel);
                }
    
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 7,
                        Name = "联想电脑",
                        BaseID = 2
                    };
                    typeList.Add(typeModel);
                }
    
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 8,
                        Name = "戴尔电脑",
                        BaseID = 2
                    };
                    typeList.Add(typeModel);
                }
    
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 9,
                        Name = "小米5手机",
                        BaseID = 3
                    };
                    typeList.Add(typeModel);
                }
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 10,
                        Name = "小米游戏电脑",
                        BaseID = 4
                    };
                    typeList.Add(typeModel);
                }
    
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 11,
                        Name = "小米5手机壳",
                        BaseID = 9
                    };
                    typeList.Add(typeModel);
                }
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 12,
                        Name = "小米游戏电脑壳",
                        BaseID = 10
                    };
                    typeList.Add(typeModel);
                }
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 13,
                        Name = "小米5手机壳【白色】",
                        BaseID = 11
                    };
                    typeList.Add(typeModel);
                }
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 14,
                        Name = "小米游戏电脑壳【亮光黑】",
                        BaseID = 12
                    };
                    typeList.Add(typeModel);
                }
                return typeList;
            }
            #endregion
        }
    
        public class TypeModel
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public Nullable<int> BaseID { get; set; }
            public string sign { get; set; }
    
        }
    }
    

    运行结果:

    二、linq无限递归获取分类

    代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace DiGui3
    {
        class Program
        {
            /// <summary>
            /// 缓存数据
            /// </summary>
            static List<TypeModel> typeList = GetList();
            /// <summary>
            /// 递归后的数据
            /// </summary>
            static List<TypeModel> RecursiveTypeList =new List<TypeModel>();
            /// <summary>
            /// 【递归深度】
            /// 如果是2级分类
            /// 一级分类是“手机”
            /// 二级分类是“小米手机”
            /// 递归第一次是“手机”深度是0
            /// 递归第二次是“小米手机”深度是1
            /// 以此类推
            /// </summary>
            static int RecursiveIndex = 0;
            static void Main(string[] args)
            {
                
                StringBuilder a = new StringBuilder();
                foreach (TypeModel t in typeList)
                {
                    a.Append($"{t.ID}{t.sign}{t.Name}\n"); 
                }
                Console.WriteLine("源数据:");
                Console.WriteLine(a);
    
               
                //递归后的数据
                Recursive();
                StringBuilder b = new StringBuilder();
                foreach (TypeModel t in RecursiveTypeList)
                {
                    b.Append($"{t.sign}{t.Name}\n");
                }
                Console.WriteLine(RecursiveIndex);
                Console.WriteLine("递归后的数据:"); 
                Console.WriteLine(b);
    
    
                Console.Read();
            }
    
    
    
            /// <summary>
            /// 递归方法
            /// </summary>
            /// <param name="id"></param> 
            private static void Recursive(int id=0)
            {
                string sign = "";//符号
                List<TypeModel> types = typeList.Where(type => type.BaseID == id).ToList();
                if (types.Count > 0)
                {
                    if (types[0].BaseID != 0)
                    {
                        RecursiveIndex++;
                    } 
                }
    
                
                for (int i = 0; i < RecursiveIndex; i++)
                {
                    sign += "-";
                }
    
                foreach (TypeModel type in types)
                { 
                    type.sign = sign; 
                    RecursiveTypeList.Add(type);
                    if (type.BaseID == 0)
                    {
                        RecursiveIndex = 0;
                        sign = "";
                    }
                    //这里需要声明一个临时变量存储当前递归深度,让这个变量去等待递归;否则,将会覆盖递归深度,造成符号数据错误,也就是 sign变量
                    int i = RecursiveIndex;
                    Recursive(type.ID);
                    //当前递归结束后把临时存储的递归深度,赋值给当前深度
                    RecursiveIndex = i;
                }
               
            }
             
    
            #region /准备数据
            private static List<TypeModel> GetList()
            {
                List<TypeModel> typeList = new List<TypeModel>();
    
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 1,
                        Name = "手机",
                        BaseID = 0
                    };
                    typeList.Add(typeModel);
                }
    
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 2,
                        Name = "电脑",
                        BaseID = 0
                    };
                    typeList.Add(typeModel);
                }
    
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 3,
                        Name = "小米手机",
                        BaseID = 1
                    };
                    typeList.Add(typeModel);
                }
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 4,
                        Name = "小米电脑",
                        BaseID = 2
                    };
                    typeList.Add(typeModel);
                }
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 5,
                        Name = "小米11手机",
                        BaseID = 3
                    };
                    typeList.Add(typeModel);
                }
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 6,
                        Name = "小米笔记本电脑",
                        BaseID = 4
                    };
                    typeList.Add(typeModel);
                }
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 7,
                        Name = "小米11的手机壳",
                        BaseID = 5
                    };
                    typeList.Add(typeModel);
                }
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 8,
                        Name = "小米笔记本plus电脑",
                        BaseID = 6
                    };
                    typeList.Add(typeModel);
                }
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 9,
                        Name = "小米11的手机壳【白色】",
                        BaseID = 7
                    };
                    typeList.Add(typeModel);
                }
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 10,
                        Name = "小米笔记本plus的电脑外壳",
                        BaseID = 8
                    };
                    typeList.Add(typeModel);
                }
    
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 11,
                        Name = "华为手机",
                        BaseID = 1
                    };
                    typeList.Add(typeModel);
                }
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 12,
                        Name = "华为电脑",
                        BaseID = 2
                    };
                    typeList.Add(typeModel);
                }
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 13,
                        Name = "华为10手机",
                        BaseID = 11
                    };
                    typeList.Add(typeModel);
                }
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 14,
                        Name = "华为10电脑",
                        BaseID = 12
                    };
                    typeList.Add(typeModel);
                }
    
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 15,
                        Name = "华为10手机壳",
                        BaseID = 13
                    };
                    typeList.Add(typeModel);
                }
    
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 16,
                        Name = "华为10电脑壳",
                        BaseID = 14
                    };
                    typeList.Add(typeModel);
                }
    
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 17,
                        Name = "华为10手机壳【黑色】",
                        BaseID = 15
                    };
                    typeList.Add(typeModel);
                }
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 18,
                        Name = "华为10电脑壳【银灰色】",
                        BaseID = 16
                    };
                    typeList.Add(typeModel);
                }
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 19,
                        Name = "空调",
                        BaseID = 0
                    };
                    typeList.Add(typeModel);
                }
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 20,
                        Name = "格力空调",
                        BaseID = 19
                    };
                    typeList.Add(typeModel);
                }
                {
                    TypeModel typeModel = new TypeModel()
                    {
                        ID = 21,
                        Name = "美的空调",
                        BaseID = 19
                    };
                    typeList.Add(typeModel);
                }
    
    
                return typeList;
            }
            #endregion
        }
    
        public class TypeModel
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public Nullable<int> BaseID { get; set; }
            /// <summary>
            /// 符号
            /// </summary>
            public string sign { get; set; }
    
        }
    }
    

    运行结果:


    三、无限分类列表-实战代码

    效果

    封装的递归代码

            #region 递归渲染列表
            /// <summary>
            /// 原数据
            /// </summary>
            private static List<Sys_Menu> PrimevalDataList = null; 
            /// <summary>
            /// 递归后的结果数据
            /// </summary>
            private static List<SysMenuBusinessModel> ReturnDataList = new List<SysMenuBusinessModel>();
    
            /// <summary>
            /// 【递归深度】 
            /// </summary>
            private static int RecursiveIndex = 0;
    
            private static void Recursive(int BaseID = 0)
            {
                RecursiveIndex = BaseID == 0 ?0: ++RecursiveIndex;
    
                //符号 
                string sign = BaseID==0? "": "&nbsp;&nbsp;";
                string BaseSign = BaseID == 0 ? "" : "<span style=\"color:#cccccc;\">&nbsp;&nbsp;&nbsp;&nbsp;└─</span>";
    
                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<SysMenuBusinessModel> resultModel = new JsonResultModel<SysMenuBusinessModel>();
    
                PrimevalDataList = bll.List(); //获取原始数据
                ReturnDataList.Clear(); //清空结果数据
                Recursive(0);//递归数据
        
    
                resultModel.count = ReturnDataList.Count;//获取结果数据的数量
                resultModel.code = 0;
                resultModel.msg = "成功";
                resultModel.data = ReturnDataList;//赋值结果数据
    
                return Json(resultModel, JsonRequestBehavior.AllowGet);
            }

    结果模型类

            /// <summary>
            /// JSON结果模型类
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// 
        public class JsonResultModel<T>
        {
            /// <summary>
            ///数据状态一切正常的状态码 0:成功,1:失败
            /// </summary>
            public int code { get; set; } = 1;
            /// <summary>
            /// 状态信息
            /// </summary>
            public String msg { get; set; } = "失败";
            /// <summary>
            /// 数据详情
            /// </summary>
            public List<T> data { get; set; }
    
            /// <summary>
            /// 数据总条数
            /// </summary>
            public int count { get; set; } = 0;
        }

    原始数据模型类 Sys_Menu

    /// <summary>
        /// 功能菜单Sys_Menu
        /// </summary>		
        public class Sys_Menu
        {
    
            /// <summary>
            /// 功能菜单ID
            /// </summary>		
            public int Sys_MenuID
            {
                get;
                set;
            }
            /// <summary>
            /// 菜单名称
            /// </summary>		
            public string Name
            {
                get;
                set;
            }
            /// <summary>
            /// 排序
            /// </summary>		
            public int Sort
            {
                get;
                set;
            }
            /// <summary>
            /// 父ID
            /// </summary>		
            public int BaseID
            {
                get;
                set;
            }
            /// <summary>
            /// 链接地址
            /// </summary>		
            public string LinkUrl
            {
                get;
                set;
            }
            /// <summary>
            /// 是否隐藏0显示1隐藏
            /// </summary>		
            public bool IsLock
            {
                get;
                set;
            }
            /// <summary>
            /// 备注说明
            /// </summary>		
            public string Remark
            {
                get;
                set;
            }
            /// <summary>
            /// 系统默认
            /// </summary>		
            public bool IsSys
            {
                get;
                set;
            }
            /// <summary>
            /// 创建人ID
            /// </summary>		
            public string CreateUserID
            {
                get;
                set;
            }
            /// <summary>
            /// 创建人姓名
            /// </summary>		
            public string CreateUserName
            {
                get;
                set;
            }
            /// <summary>
            /// 创建时间
            /// </summary>		
            public DateTime CreateDateTime
            {
                get;
                set;
            }
            /// <summary>
            /// 修改人ID
            /// </summary>		
            public string UpdateUserID
            {
                get;
                set;
            }
            /// <summary>
            /// 修改人
            /// </summary>		
            public string UpdateUserName
            {
                get;
                set;
            }
            /// <summary>
            /// 修改时间
            /// </summary>		
            public DateTime UpdateDateTime
            {
                get;
                set;
            }
            /// <summary>
            /// 是否删除
            /// </summary>		
            public bool IsDelete
            {
                get;
                set;
            }
    
        }

    结果数据模型类 

        /// <summary>
        /// 菜单业务模型类
        /// </summary>
        public class SysMenuBusinessModel:Sys_Menu
        {
            public string BusinessName { set; get; } //业务名称,递归封装后的名称
        }

    四、根据第三示例实现完美封装

    递归渲染列表

    #region 递归渲染列表
            /// <summary>
            /// 原数据
            /// </summary>
            private static List<Sys_Menu> PrimevalDataList = null;
            /// <summary>
            /// 递归后的结果数据
            /// </summary>
            private static List<SysMenuBusinessModel> ReturnDataList = new List<SysMenuBusinessModel>();
    
            /// <summary>
            /// 【递归深度】 
            /// </summary>
            private static int RecursiveIndex = 0;
    
            /// <summary>
            /// 递归循环
            /// </summary>
            /// <param name="_BaseID">父级ID</param>
            /// <param name="intervalSign">间隔符</param>
            /// <param name="markSign">标识符</param>
            /// <param name="intervalSignIsTop">间隔符是否在前</param>
            private static void Recursive(int BaseID = 0, string intervalSign = "&nbsp;&nbsp;", string markSign = "<span style=\"color:#cccccc;\">&nbsp;&nbsp;&nbsp;&nbsp;└─</span>", 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;//把递归渲染好的数据赋值给返回的对象变量

    展开全文
  • function mainclc; clear all; close all;N=20;k=0;Pi1=[0;0.318757112730057;0.213298358723121;0.142153563503059;0.0834036754403426;0.0478257345954396;0.0270339589939721;...0.0084548690802...

    function main

    clc; clear all; close all;

    N=20;

    k=0;

    Pi1=[0;0.318757112730057;0.213298358723121;0.142153563503059;0.0834036754403426;0.0478257345954396;0.0270339589939721;0.0151400172456431;0.00845486908021234;0.00467820500152320;0.00258209730449347;0.00142230585761635;0.000782154535688881;0.000429523417716290;0.000235594767478955;0.000129092241742530;7.06720590175056e-05;3.86594249422924e-05;2.11330394453352e-05;1.15451622357423e-05;6.30372380330690e-06;3.44014615058444e-06;1.87654692109411e-06;1.02320420506109e-06;5.57700269968980e-07;3.03870447486477e-07;1.65514859822981e-07;9.01274183424514e-08;4.90634001263182e-08;2.67022275422694e-08;1.45289423684672e-08;7.90358168523524e-09;4.29856462521843e-09;2.33742728864112e-09;1.27078758315683e-09;6.90768691704174e-10;3.75423447956114e-10;2.04006072980306e-10;1.10841227775878e-10;6.02142646666072e-11;3.27069706546436e-11;1.77634350206324e-11;9.64632512844664e-12;5.23778401619916e-12;2.84371643188436e-12;1.54376116732191e-12;8.37974943939880e-13;4.54821405204166e-13;2.46837664624152e-13;1.33950473967085e-13;7.26843476240200e-14;3.94369017465178e-14;2.13959354621392e-14;1.16072046510403e-14;6.29641111719542e-15;3.41529891596401e-15;1.85240372255083e-15;1.00465018394328e-15;5.44837953476417e-16;2.95456816368876e-16;1.60212249730676e-16;8.68706944137495e-17];

    h=0.8;

    D_eo=7;

    D_p=8;

    D_f=10;

    x_max=50;

    pro=P_U_test(N,k,Pi1,h,D_eo,D_p,D_f,x_max)

    function pro=P_U_test(N,k,Pi1,h,D_eo,D_p,D_f,x_max)

    %%%***测试参数**********

    %N=2;

    %k=0;

    %Pi1=[0;0.318757112730057;0.213298358723121;0.142153563503059;0.0834036754403426;0.0478257345954396;0.0270339589939721;0.0151400172456431;0.00845486908021234;0.00467820500152320;0.00258209730449347;0.00142230585761635;0.000782154535688881;0.000429523417716290;0.000235594767478955;0.000129092241742530;7.06720590175056e-05;3.86594249422924e-05;2.11330394453352e-05;1.15451622357423e-05;6.30372380330690e-06;3.44014615058444e-06;1.87654692109411e-06;1.02320420506109e-06;5.57700269968980e-07;3.03870447486477e-07;1.65514859822981e-07;9.01274183424514e-08;4.90634001263182e-08;2.67022275422694e-08;1.45289423684672e-08;7.90358168523524e-09;4.29856462521843e-09;2.33742728864112e-09;1.27078758315683e-09;6.90768691704174e-10;3.75423447956114e-10;2.04006072980306e-10;1.10841227775878e-10;6.02142646666072e-11;3.27069706546436e-11;1.77634350206324e-11;9.64632512844664e-12;5.23778401619916e-12;2.84371643188436e-12;1.54376116732191e-12;8.37974943939880e-13;4.54821405204166e-13;2.46837664624152e-13;1.33950473967085e-13;7.26843476240200e-14;3.94369017465178e-14;2.13959354621392e-14;1.16072046510403e-14;6.29641111719542e-15;3.41529891596401e-15;1.85240372255083e-15;1.00465018394328e-15;5.44837953476417e-16;2.95456816368876e-16;1.60212249730676e-16;8.68706944137495e-17];

    %h=0.8;

    %D_eo=7;

    %D_p=8;

    %D_f=10;

    %x_max=50;

    %%%***测试参数**********

    if N==1

    if k==0

    pro=my_trapz(0,D_eo,Pi1,h);%%单部件系统没有维修需求的概率;

    else

    if k==1

    pro=my_trapz(D_eo,D_p,Pi1,h);%%单部件系统被外部机会维修的概率

    end

    end

    else

    if k==0

    pro=P_U_test(N-1,0,Pi1,h,D_eo,D_p,D_f,x_max)*...%

    P_U_test(1,0,Pi1,h,D_eo,D_p,D_f,x_max);

    else

    if k==N

    pro=P_U_test(N-1,N-1,Pi1,h,D_eo,D_p,D_f,x_max)*...%

    P_U_test(1,1,Pi1,h,D_eo,D_p,D_f,x_max);

    else %% P_N_Uk (0

    %             if k>0 & k

    pro=P_U_test(N-1,k,Pi1,h,D_eo,D_p,D_f,x_max)*...%%

    P_U_test(1,0,Pi1,h,D_eo,D_p,D_f,x_max)+...

    P_U_test(N-1,k-1,Pi1,h,D_eo,D_p,D_f,x_max)*...

    P_U_test(1,1,Pi1,h,D_eo,D_p,D_f,x_max);

    end

    end

    end

    function p=my_trapz(a,b,Pi1,h)%%

    a=ceil((a+h)./h);%%

    b=floor(b./h);%%;

    p=0;

    for i=a:b

    p=p+Pi1(i);%%

    end

    p=p*h;%%

    展开全文
  • 可能的原因是程序内存在无限递归。 出错 netcdf.open (line 63) [varargout{:}] = netcdf.open(filename, varargin{1} ); 代码如下: switch nargin case 1 [varargout{:}] = netcdf.open(filename, 'NOWRITE' ); ...
  • 主要介绍了JavaScript实现无限级递归树的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
  • 当我从User调用findById方法时,会收到一个结果,但是当我尝试从webservice转换返回时,会抛出此异常:org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference ...

    当我从User调用findById方法时,会收到一个结果,但是当我尝试从webservice转换返回时,会抛出此异常:

    org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.empsys.user.User[“locations”]->org.hibernate.collection.internal.PersistentBag[0]->com.empsys.contact.details.Location[“contact”]->com.empsys.user.User[“locations”]->org.hibernate.collection.internal.PersistentBag[0]->com.empsys.contact.details.Location[“contact”]->com.empsys.user.User[“locations”]->org.hibernate.collection.internal.PersistentBag[0]->com.empsys.contact.details.Location[“contact”]->com.empsys.user.User[“locations”]->org.hibernate.collection.internal.PersistentBag[0]->com.empsys.contact.details.Location[“contact”] …

    课程关系是:

    类联系 – 创建此类是为了表示系统上的多种联系人类型.

    @Entity

    @Table(name = "CONTACT", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})

    @Inheritance(strategy = InheritanceType.JOINED)

    @DiscriminatorColumn(name = "CONTACT_TYPE")

    public class Contact implements Serializable {

    @Id

    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private Long id;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="contact")

    @Column(nullable = true)

    @JsonManagedReference

    private List locations;

    ...

    类用户 – 创建此类以表示具有特定信息的用户

    @Entity

    @Table(name = "USER")

    @DiscriminatorValue("USER")

    public class User extends Contact {

    ...

    类位置 – 创建此类是为了表示用户和系统其他联系人的地址

    @Entity

    @Table(name = "LOCATION", uniqueConstraints = {@UniqueConstraint(columnNames = {"id"})})

    public class Location implements Serializable {

    @ManyToOne

    @JoinColumn(name = "contact_id", nullable = false)

    @JsonBackReference

    private Contact contact;

    ...

    使用的依赖关系:

    >(com.sun.jersey)jersey-json:版本1.18

    >(com.fasterxml.jackson.datatype)jackson-datatype-hibernate4:version:

    2.4.0

    谁能帮助我?

    展开全文
  • } /// /// 无限递归 /// /// /// [NonAction] public List GetTreeVos(List GetModelList, string parentId) { List treeVos = new List(); foreach (var gpml in GetModelList) { TreeVo treeVo = new TreeVo(); ...

    废话不多说直接上代码,先看 层级包装实体类

     public class TreeVo
        {
            public string id { set; get; }
            public string name { set; get; }
            public object children { set; get; }
    
        }

     下面是数据承载类

    public class MemberRank{
    	    private string _mrankcode;
    		private string _node="";
    		private string _parentnode="";
    		private string _mrankname;
    		private string _short="";
    		private int _delflag=0;
    }

    下面看结果

    
            public CommonResult QueryTree()
            {
    
                
                List<SelfHelpPay.Models.Po.memberRank> GetModelList = memberRankService.GetModelList();
              
                List<TreeVo> treeVos = new List<TreeVo>();
                foreach (var item in GetModelList)
                {
                    if (string.IsNullOrEmpty( item.parentNode))
                    {
                    TreeVo treeVo = new TreeVo();
                    treeVo.id = item.MRankCode;
                    treeVo.name = item.MRankName;
                    treeVo.children = GetTreeVos(GetModelList, item.node);
                    treeVos.Add(treeVo);
                    }
                }
               
                return commonResult.success(treeVos);
            }
    
           /// <summary>
           /// 无限递归
           /// </summary>
           /// <param name="treeVos"></param>
           /// <returns></returns>
           [NonAction]
            public List<TreeVo> GetTreeVos(List<SelfHelpPay.Models.Po.memberRank> GetModelList, string parentId) {
    
                List<TreeVo> treeVos = new List<TreeVo>();
                foreach (var gpml in GetModelList) {
                    TreeVo treeVo = new TreeVo();
                    treeVo.id = gpml.MRankCode;
                    treeVo.name = gpml.MRankName;
                    if (gpml.parentNode.Equals(parentId))
                    {
                        treeVo.children= GetTreeVos(GetModelList, gpml.node);
                        treeVos.Add(treeVo);
                    }
                  
                }
                    return treeVos;
            }

    展开全文
  • 第一种情况就是正常的递归算法,解除递归限制: import sys sys.setrecursionlimit(1000000) 1.那什么是递归算法呢,满足了什么条件就是递归算法呢? 必须的条件: 一、函数调用自身 二、设置了正确的返回条件 2. ...
  • 1、无限级递归查找下级 (1)调用 public function tree(){ $id = (I('get.id')); $xiaji = Db::name('users')->where('FIND_IN_SET('.$id.',leader_arr)')->field('user_id,nickname,head_pic,level,...
  • 使用.NetCore5 EF来实现无限级树形结构查询,操作很简单,直接查询即可,可以使用FromSqlRaw或者普通的查询都可以。查询结果.ToList(),再过滤掉不符合条件的项目。查询时不要使用AsNoTracking,否则不能生成下级...
  • jpa实体 @ManyToOne @OneToMany无限递归问题描述解决办法 问题描述 Cannot call sendError() after the response has been committed 解决办法 在属性上添加注释 @JsonBackReference // data to json 和 ...
  • 今天小编就为大家分享一篇tp5递归 无限级分类详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
  • 下面小编就为大家带来一篇Java无限级树(递归)超实用案例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
  • Java 无限级递归生成树级菜单

    千次阅读 2019-04-10 21:15:19
    如代码所示,每次将一个子节点作为父节点,递归调用查找子节点方法,直到所有的子节点都找完,返回主方法,递归结束。  三、结果展示 { "menus": [ [ { "code": "P2300", "i18nid": 361, "useflag": ...
  • 无限递归引发的堆栈溢出

    千次阅读 2018-04-10 20:50:58
    今天在写strlen函数的递归实现,当执行以下代码时,会出现段错误。 #include&lt;assert.h&gt; size_t my_strlen(char *str) { assert(str != NULL); if(*str == '\0') { return 0; } else { ...
  • json序列化导致无限递归的问题

    千次阅读 2019-03-15 15:16:46
    遇到个比较奇葩的问题,在序列化一个实体类时,总是报无限递归导致堆溢出的错误,核查后发现,原来我这个实体里面 有一个Method的熟悉,而Method中有一个自己的实体属性Method root,所以导致序列化的时候不停地递归...
  • swagger出参对象无限递归解决

    千次阅读 2019-07-29 15:34:49
    例如:如下的出参 会出现加载不出来的情况,如下图: 解决方式1: ...在造成无限递归的这里,隐藏掉属性,就不会有error提示,但是文档上相应的会少了这一项的,不过,运行测试返回是正常的。 ...
  • //循环所有根目录的分享数据,调用递归,去查询所有根目录下的子级分享数据(递归也是通过父级ID去定位查询的) foreach (var item in lis) { //查询出来的子级集合,赋值给当前对象的子级集合属性中。 List...
  • 无限递归(与无限循环一样)

    千次阅读 2018-06-15 18:16:19
    public int sumByMax(int max){ if(max&lt;=2){ return max+sumByMax(max-1); }else{ return max; }}
  • java 无限递归遍历json对象

    千次阅读 2020-09-22 22:30:56
    需求 给一个json字符串,遍历json字符串,输出其每个key对应的value。举一个例子: ...1、采用递归的方法打印每个路径上的值: static final String json_schema2 = "{\"a\":1,\"b\":{\"b1\":2
  • vue 无限递归级联组件实现方案

    千次阅读 2018-09-20 14:10:00
     在这里有一个很重要的地方就是前端组件如何与后端匹配方法协调好,无限级联很好实现,但是如何让服务器端可以成功的匹配到条件是一个问题,在这里我借鉴了html元素的思想,将每一个下拉框作为一个条件主体,将下拉框...
  • C# TreeView无限递归绑定 CheckBox(vs2010)可运行 C# TreeView无限递归绑定 CheckBox 加入 CheckBox 事件
  • 无限递归导致StackOverflowError

    千次阅读 2017-07-20 16:14:24
    因为涉及到了一个回调所以就写了一个接口用于将当前的上传状态通知外界,不幸的是我自己写的那个回调方法与uploadManager.put方法中的回调方法的命名是相同的,这也就导致了后面在调用的时候就变成了递归调用,一直...
  • 无限递归循环编译器是如何处理的

    千次阅读 2017-12-18 18:22:00
    不管是C语言还是python ,都支持递归调用,就是函数call 自己,那什么时候,才是个头呢?就是说,什么时候,才跳出循环呢? consider the following code: def foo(): foo() try: foo() except RuntimeError: ...
  • 关于__getattribute__和__getattr__的区别,请参照:http://www.cnblogs.com/bettermanlu/archive/2011/06/22/2087642.html 使用object作为基类,若想调用self.key则替换为object.__getattribute__(self,key) ...
  • 如果在LinkController中直接查询Link对象会出现json无限递归问题。 @JsonIdentityInfo解决 分别在父子类中添加如下注释: BusinessTemplate.java @JsonIdentityInfo(generator = ObjectIdGenerators.Property...
  • 关于递归的问题:无限递归

    千次阅读 2020-04-14 08:57:02
    当你在写递归的时候,可能会出现运行不出结果来,此时你应该试着看看会不会出现无限递归的问题,如果有的话,你可以在前面增加一个判断语句,满足条件进入执行相关操作与递归,但在递归的时候,你应该注意不要在里面...
  • 网上搜了很多解决办法,解决起来还是很简单的,做一下记录。 最推荐的是 ...@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator....避免了无限循环查询的情况。这个注解我觉得还是比较无脑的。
  • jackson转换json出现无限递归问题

    千次阅读 2015-02-23 23:07:26
    在controller返回数据到统一json转换的时候,出现了json infinite recursion stackoverflowerror的错误,即json在将对象转换为json格式的数据的时候,出现了无限递归调用的情况。   具体的情况如下:   A类中...
  • jackson中的@JsonBackReference和@JsonManagedReference,以及@JsonIgnore均是为了解决对象中存在双向引用导致的无限递归问题. @JsonBackReference和@JsonManagedReference:这两个标注通常配对使用,通常用在父子...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 75,643
精华内容 30,257
关键字:

无限递归