常用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; }
待补充
作者:chenze 出处:https://www.cnblogs.com/chenze-Index/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 如果文中有什么错误,欢迎指出。以免更多的人被误导。 |