zoukankan      html  css  js  c++  java
  • 【转】C# 中Linq查询所有上级节点或所有下级节点

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net.Http;
    using System.Text;
    using System.Threading.Tasks;
     
    namespace ConsoleApp7
    {
        class Program
        {
            static void Main(string[] args)
            {
     
                var data = GetData();
     
                //获取所有下级
                //var query = GetSons(data, 1);
     
                //获取所有上级
                var query = GetFatherList(data, 5);
                Console.WriteLine("Id	 区划	 父ID	 层级	");
                query.ToList().ForEach(q => Console.WriteLine("{0}	 {1}	 {2}	 {3}	", q.Id, q.Name, q.Fid, q.Level));
     
                Console.ReadLine();
            }
     
            #region 测试数据
            public static IList<menu> GetData()
            {
                var list = new List<menu>();
                list.Add(new menu { Id = 1, Name = "广东省", Fid = 0, Level = 1 });
                list.Add(new menu { Id = 2, Name = "深圳市", Fid = 1, Level = 2 });
                list.Add(new menu { Id = 3, Name = "南山区", Fid = 2, Level = 3 });
                list.Add(new menu { Id = 4, Name = "福田区", Fid = 2, Level = 3 });
                list.Add(new menu { Id = 5, Name = "上梅林", Fid = 4, Level = 4 });
                list.Add(new menu { Id = 6, Name = "下梅林", Fid = 4, Level = 4 });
                list.Add(new menu { Id = 7, Name = "车公庙", Fid = 4, Level = 4 });
                list.Add(new menu { Id = 8, Name = "蛇口", Fid = 5, Level = 4 });
                list.Add(new menu { Id = 9, Name = "科技园", Fid = 5, Level = 4 });
                list.Add(new menu { Id = 10, Name = "湖南省", Fid = 0, Level = 1 });
                list.Add(new menu { Id = 11, Name = "长沙市", Fid = 10, Level = 2 });
                list.Add(new menu { Id = 12, Name = "芙蓉区", Fid = 11, Level = 3 });
                return list;
            }
            #endregion
     
            #region 获取所有下级
            public static IEnumerable<menu> GetSons(IList<menu> list, int Fid)
            {
                var query = list.Where(p => p.Id == Fid).ToList();
                var list2 = query.Concat(GetSonList(list, Fid));
                return list2;
            }
     
            public static IEnumerable<menu> GetSonList(IList<menu> list, int Fid)
            {
                var query = list.Where(p => p.Fid == Fid).ToList();
                return query.ToList().Concat(query.ToList().SelectMany(t => GetSonList(list, t.Id)));
            }
            #endregion
     
            #region 获取所有上级
            public static IEnumerable<menu> GetFatherList(IList<menu> list, int Id)
            {
                var query = list.Where(p => p.Id == Id).ToList();
                return query.ToList().Concat(query.ToList().SelectMany(t => GetFatherList(list, t.Fid)));
            }
            #endregion
     
            #region 实体类
            public class menu
            {
                public int Id { set; get; }
                public string Name { set; get; }
                public int Fid { set; get; }
                public int Level { set; get; }
            }
            #endregion
        }
    }
    

     转自:https://blog.csdn.net/smartsmile2012/article/details/81317327

  • 相关阅读:
    【机器学习】:梯度提升决策树(GBDT)
    【推荐系统】:LFM算法解析
    【SQL】:内连接,自然连接
    【SQL】:保留小数点后几位(除法)
    Ubuntu 18.04:磁盘读取性能不佳
    skynet超时机制实现
    关于 Spring Boot 中创建对象的疑虑 → @Bean 与 @Component 同时作用同一个类,会怎么样?
    记录不存在则插入,存在则更新 → MySQL 的实现方式有哪些?
    记一次线上问题 → 对 MySQL 的 ON UPDATE CURRENT_TIMESTAMP 的片面认知
    WebSocket
  • 原文地址:https://www.cnblogs.com/hycms/p/10502225.html
Copyright © 2011-2022 走看看