Net Core教程

C# 递归将n级实体类转换为一维数据源

本文主要是介绍C# 递归将n级实体类转换为一维数据源,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
/// <summary>
        /// 将父子级数据结构转换为普通list
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public static List<UniWater_MenuInfo> GetMenuInfoList(List<UniWater_MenuInfo> list)
        {
            List<UniWater_MenuInfo> Resultlist = new List<UniWater_MenuInfo>();
            foreach (var item in list)
            {
                OperationChildData(Resultlist, item);
                Resultlist.Add(item);
            }
            return Resultlist;
        }
        /// <summary>
        /// 递归子级数据
        /// </summary>
        /// <param name="treeDataList">树形列表数据</param>
        /// <param name="parentItem">父级model</param>
        public static void OperationChildData(List<UniWater_MenuInfo> AllList, UniWater_MenuInfo item)
        {
            if (item.children != null)
            {
                if (item.children.Count > 0)
                {
                    AllList.AddRange(item.children);
                    foreach (var subItem in item.children)
                    {
                        OperationChildData(AllList, subItem);
                    }
                }
            }
        }

转自:http://www.manongjc.com/detail/17-cwfkfdaeqrbjqyr.html

这篇关于C# 递归将n级实体类转换为一维数据源的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!