C#树结构操作逻辑整理
来源:互联网 发布:希尔薇 知乎 编辑:程序博客网 时间:2024/05/16 00:39
1.向下查询
/// <summary> /// --1.指定ID ,向下查询所有的子节点------递归获取 /// </summary> /// <param name="id"></param> public List<Tree> getChild(int id) { List<Tree> list = new List<Tree>(); //根据NodeID,获取当前子节点列表 List<Tree> chidList = _Context.Trees.Where(q => q.ParentID == id).ToList(); if (chidList.Count > 0) { //获取下一级 list.AddRange(chidList); foreach (var item in chidList) { //递归获取下一级 chidList = getChild(item.NodeID); list.AddRange(chidList); } } return list; } /// <summary> /// --1.指定ID ,向下查询所有的子节点----死循环获取 /// </summary> /// <param name="id"></param> public List<Tree> getChild2(int id) { List<Tree> list = new List<Tree>(); List<int> idList = new List<int>() { id }; while (true) { //1.获取子节点列表 List<Tree> childList = _Context.Trees.Where(q => idList.Contains(q.ParentID)).ToList(); if (childList.Count <= 0) break; list.AddRange(childList); //2.遍历子节点 ID列表 idList = childList.Select(q => q.NodeID).ToList(); if (idList.Count <= 0) break; } return list; }
2.向上查询
/// <summary> /// --2.指定ID,向上查询所有的父级节点----递归获取 /// </summary> public List<Tree> getParent(int id) { List<Tree> list = new List<Tree>(); //获取当前项 Tree tree = _Context.Trees.Where(q => q.NodeID == id).FirstOrDefault(); if (tree != null) { //获取上一级 Tree parent = _Context.Trees.Where(q => q.NodeID == tree.ParentID).FirstOrDefault(); if (parent != null) { list.Add(parent); //递归调用 获取上一级 list.AddRange(getParent(parent.NodeID)); } } return list; } /// <summary> /// --2.指定ID,向上查询所有的父级节点----死循环获取 /// </summary> public List<Tree> getParent2(int id) { List<Tree> list = new List<Tree>(); while (true) { //1.获取当前项 Tree tree = _Context.Trees.Find(id); if (tree == null) break; //2.修改下一次获取上一级 list.Add(tree); id = tree.ParentID; } return list; }
3.获取其他同级节点
/// <summary> /// --3,指定ID,获取其他同级节点 /// </summary> public List<Tree> getSiblings(int id) { List<Tree> list = new List<Tree>(); //1.获取当前项的上级ID Tree tree = _Context.Trees.Find(id); if (tree != null) { //2.获取上一级下的所有节点,过滤掉自己 list.AddRange(_Context.Trees.Where(q => q.ParentID == tree.ParentID && q.NodeID != id)); } return list; }
4.是否是子节点
/// <summary> /// --3.指定ParentID,判断TargetID,是否是子节点 /// </summary> public bool isChild(int parentID, int targetID) { //1.过滤 相同情况 if (parentID == targetID) return false; List<int> idList = new List<int>() { parentID }; while (true) { //2.获取子节点列表 List<Tree> childList = _Context.Trees.Where(q => idList.Contains(q.ParentID)).ToList(); if (childList.Count <= 0) return false; //3.判断是否是子节点 if (childList.Any(q => q.NodeID == targetID)) return true; //4.重置子节点ID 列表 idList = childList.Select(q => q.NodeID).ToList(); } }
5.是否是父节点
/// <summary> /// --4.指定ChildID,判断TargetID,是否是父节点 /// </summary> public bool isParent(int childID, int targetID) { //1.过滤相同情况 if (childID == targetID) return false; while (true) { //1.获取当前项 Tree tree = _Context.Trees.Find(childID); if (tree == null) return false; //2.判断是否是父节点 if (tree.ParentID == targetID) return true; //3.重置字节ID childID = tree.ParentID; } }
0 0
- C#树结构操作逻辑整理
- Postgresql逻辑结构整理
- C#操作结构体
- 二叉树逻辑结构重点
- c#操作excel文件整理
- C#操作XML代码整理
- C#中文件操作整理
- c#中指针操作结构
- 逻辑结构操作(表空间,数据文件,段,块,区)
- 2.1 线性表的定义、逻辑结构、基本操作
- C#基础知识整理:C#类和结构(1)
- C#基础知识整理:C#类和结构(2)
- C#基础知识整理:C#类和结构(3)
- C#基础知识整理:C#类和结构(4)
- C#基础知识整理:C#类和结构(1)
- 数据结构(三):非线性逻辑结构-树
- C#操作Excel开发报表系列整理
- C#操作Excel开发报表系列整理
- SGU 102. Coprimes
- 使用两个辅助的队列和非数组变量设计一个算法以使队列中的元素有序
- 《C#高级编程》【第三章】对象和类型 -- 学习笔记
- DuiVision开发教程(1)-创建DuiVision工程
- IOS NSUserDefaults-轻量级本地数据存储
- C#树结构操作逻辑整理
- uml精粹——5.类图(高级概念)
- TCP粘包
- 自定义UI-TextView实现文本自动滚动显示
- 字母大小写分类排序输出
- 一个女人13小时内拍的两张照片,所有人看完都沉默了~
- android初学笔记1
- 将栈S1中的元素复制到栈s2中,并保持顺序不变,使用额外的一个栈
- 从HTTP说起,肆意漫谈