递归父子关系 C#

Recursive Parent-Child Relationship C#

我正在尝试从 .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 输入定义

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

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

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

要放入层级的项目

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();
}

试试这个:

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");

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

建立父关系
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();