【问题标题】:Recursive Parent-Child Relationship C#递归父子关系 C#
【发布时间】:2017-01-18 05:19:49
【问题描述】:

我正在尝试从 .csv 文件构建层次结构,它由字符串中的 ID(功能位置)、描述和父 ID(SupFunctLoc)组成。我已将数据提取到列表中。 代码和数据示例供参考。

 Functional Loc.    Description SupFunctLoc.
 70003  ABC AS002
 70C2   ABC 70003
 70C2.01    ABC 70C2
 70C2.01.02 ABC 70C2.01
 70C2.01.02.10  ABC 70C2.01.02
 70C2.01.02.10-BG010    ABC 70C2.01.02.10

示例代码:

static void Main(string[] args)
    {
        List<Input> inputList = new List<Input>();
        var yourData = File.ReadAllLines(locate)
               .Skip(1)
               .Select(x => x.Split(','))
               .Select(x => new Input()
               {
                   FunctionalLocation = x[0],
                   Description = x[1],
                   SuppFunctionalLocation = x[2],

               });
        //try 3

        //try 2

        var outputList = yourData
            .Where(i => i.SuppFunctionalLocation!= null) // Just get the parents
            .Select(i => new Input()
            {
                Description = i.Description,
                SuppFunctionalLocation = i.SuppFunctionalLocation,
                Children = inputList
                    .Where(x => x.FunctionalLocation.ToString() == i.SuppFunctionalLocation.ToString())
                    .Select(x => new Input()
                    {
                        Description = x.Description,
                        SuppFunctionalLocation = x.SuppFunctionalLocation,
                        FunctionalLocation = x.FunctionalLocation,
                    }).ToList()
                }).ToList();


        foreach (var output in outputList)
        {
            Console.WriteLine(output.Description);
            output.Children.ForEach(c => Console.WriteLine($"\t {c.Description}"));
        }
}

输入的类定义

 class Input
    {
        public string FunctionalLocation { get; set; }
        public string Description { get; set; }
        public string SuppFunctionalLocation { get; set; }
        public List<Input> Children { get; set; }
    }

请帮助在这种情况下可以做些什么。感谢您的帮助。

【问题讨论】:

  • 您正在拆分 ,,但您的示例文本没有逗号。此外,您需要为Input 提供类def。
  • @Enigmativity 你好我已经粘贴了一个数据样本并使用类输入定义更新了问题
  • Functional Loc. 是主键吗?
  • foreach (var item in collection) item.Children = collection.Where( c => c.ParentId == item.Id ).ToList();
  • 还有:为什么我有一个孩子的名单?据我所知,总是只有一个 supfunc...

标签: c# recursion hierarchy


【解决方案1】:

将所有项目读入集合后,与

建立父关系
foreach ( var parent in inputlist )
{
    parent.Children = inputlist
        .Where( child => child.SuppFunctionalLocation == parent.FunctionalLocation )
        .ToList();
}

要仅获取根元素,您必须查找所有没有父项的项目

var outputlist = inputlist
    .Where( child => !inputlist.Any( parent => parent.FunctionalLocation == child.SuppFunctionalLocation ) )
    .ToList();

【讨论】:

    【解决方案2】:

    试试这个:

    var source = @"Functional Loc.    Description SupFunctLoc.
    70003  ABC AS002
    70C2   ABC 70003
    70C2.01    ABC 70C2
    70C2.01.02 ABC 70C2.01
    70C2.01.02.10  ABC 70C2.01.02
    70C2.01.02.10-BG010    ABC 70C2.01.02.10";
    
    var lines =
        source
            .Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
            .Select(x => x.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
    
    var results =
        lines
            .Skip(1)
            .Select(x => new Input()
            {
                FunctionalLocation = x[0],
                Description = x[1],
                SuppFunctionalLocation = x[2],
            });
    
    var lookup = results.ToLookup(x => x.SuppFunctionalLocation);
    
    Func<string, List<Input>> build = null;
    build = SuppFunctionalLocation =>
        lookup[SuppFunctionalLocation]
            .Select(x => new Input()
            {
                FunctionalLocation = x.FunctionalLocation,
                Description = x.Description,
                SuppFunctionalLocation = x.SuppFunctionalLocation,
                Children = build(x.FunctionalLocation),
            })
            .ToList();
    
    List<Input> tree = build("AS002");
    

    【讨论】:

      【解决方案3】:

      从具有现有父键的项目构建层次结构相对容易。我从你的示例中抽象了一点,但基本上是一样的。

      要放入层次结构的项目

      public class MyItem
      {
          public string Id { get; set; }
          public string Description { get; set; }
          public string ParentId { get; set; }
          // to be filled
          public IList<MyItem> Children { get; set; }
      }
      

      构造子列表的代码。

      // assumption: MyItem elements have all properties set except the children collection
      ICollection<MyItem> items = GetMyItems();
      
      var parentRelation = items.ToLookup(x => x.ParentId);
      foreach (var item in items)
      {
          item.Children = parentRelation[item.Id].ToList();
      }
      

      【讨论】:

      • 非常感谢... :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-10
      • 2013-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多