评论

收藏

[R语言] Recursion之Demo

编程语言 编程语言 发布于:2021-07-30 18:23 | 阅读数:540 | 评论:0

Model:
public class PerSon
  {
    [DisplayName("标识id")]
    public string id { get; set; }
    [DisplayName("名称")]
    public string name { get; set; }
    [DisplayName("年龄")]
    public int age { get; set; }
    [DisplayName("父级")]
    public int? PerSonId { get; set; }
    /// <summary>
    /// 该分类的子分类
    /// </summary>
    public List<PerSon> items { get; set; }
  }
Code:
class Program
  {
    static List<PerSon> list = new List<PerSon>()
      {
        new PerSon(){ id = "1", age=21, name="张飞1", PerSonId = null },
        new PerSon(){ id = "2", age=22, name="张飞7", PerSonId = null },
        new PerSon(){ id = "3", age=23, name="张飞8", PerSonId = null },
        new PerSon(){ id = "4", age=24, name="张飞5", PerSonId = 1 },
        new PerSon(){ id = "5", age=25, name="张飞4", PerSonId = 2 },
        new PerSon(){ id = "6", age=27, name="张飞3", PerSonId = 3 },
        new PerSon(){ id = "7", age=28, name="张1", PerSonId = 3 },
        new PerSon(){ id = "8", age=29, name="张2", PerSonId = 4 },
        new PerSon(){ id = "9", age=30, name="张飞1", PerSonId = 5 },
      };
    static void Main(string[] args)
    {
      var ViewData = new List<PerSon>();
      list.ForEach((u) =>
      {
        if (u.PerSonId == null)
        {
          ViewData.Add(new PerSon()
          {
            age = u.age,
            id = u.id,
            name = u.name,
            items = getitem(u.id)
          });
        }
      });
      var a = ViewData;
    }
    public static List<PerSon> getitem(string id)
    {
      List<PerSon> perSons = new List<PerSon>();
      foreach (var items in list)
      {
        if (items.PerSonId.ToString() == id)
        {
          perSons.Add(new PerSon()
          {
            age = items.age,
            id = items.id,
            name = items.name,
            PerSonId = Convert.ToInt32(id),
            items = getitem(items.id)
          });
        }
      }
      return perSons;
    }
  }


关注下面的标签,发现更多相似文章