Let's go

常用C#处理

1.

List<string> planIDList = new List<string>();
planIDList.Add(strPlanid);
planIDList = planIDList.Distinct().ToList();//去重

2. 常用树形结构数据处理

 public List<MenuEntity> buildMenuTree(List<MenuEntity> menus)
        {
            List<MenuEntity> returnList = new List<MenuEntity>();
            List<int> tempList = new List<int>();
            foreach (var item in menus)
            {
                tempList.Add(item.id);
            }
            foreach (var item in menus)
            {
                if (!tempList.Contains(item.pid))
                {
                    recursionFn(menus, item);
                    returnList.Add(item);
                }
            }
            if (returnList.Count == 0)
            {
                returnList = menus;
            }
            return returnList;
        }


        public void recursionFn(List<MenuEntity> list, MenuEntity t)
        {
            List<MenuEntity> childList = getChildList(list, t);
            t.children = childList;
            foreach (MenuEntity tChild in childList)
            {
                if (hasChild(list, tChild))
                {
                    recursionFn(list, tChild);
                }
            }
        }

        /// <summary>
        /// 得到子节点列表
        /// </summary>
        /// <param name="list"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public List<MenuEntity> getChildList(List<MenuEntity> list, MenuEntity t)
        {
            List<MenuEntity> tlist = new List<MenuEntity>();
            foreach (var item in list)
            {
                if (item.pid == t.id)
                {
                    tlist.Add(item);
                }
            }
            return tlist;
        }

        /// <summary>
        /// 判断是否有子节点
        /// </summary>
        /// <param name="list"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        private bool hasChild(List<MenuEntity> list, MenuEntity t)
        {
            return getChildList(list, t).Count() > 0 ? true : false;
        } 

待补充

posted @ 2020-09-13 12:08  chenze  阅读(126)  评论(0编辑  收藏  举报
有事您Q我