使用linq合并父子属性

时间:2022-03-05 08:19:10

Is there a LINQ way to accomplish this? Any help is greatly appreciated.

是否有LINQ方法来实现这一目标?任何帮助是极大的赞赏。

class Program
{
    static void Main(string[] args)
    {
        Parent parent1 = new Parent();
        parent1.Name = "P1";


        Parent parent2 = new Parent();
        parent2.Name = "P2";


        Child child1 = new Child();
        child1.Name = "C1";

        Child child2 = new Child();
        child2.Name = "C2";

        Child child3 = new Child();
        child3.Name = "C3";

        Child child4 = new Child();
        child4.Name = "C4";


        parent1.Children.Add(child1);
        parent1.Children.Add(child2);


        parent2.Children.Add(child3);
        parent2.Children.Add(child4);

        List<Parent> parentCollection = new List<Parent>();
        parentCollection.Add(parent1);
        parentCollection.Add(parent2);



        List<string> nameCollection = new List<string>();


        foreach( Parent parent in parentCollection){
            nameCollection.Add(parent.Name);
            foreach(Child child in parent.Children)
                nameCollection.Add(child.Name);

        }

    }
}


public class Parent
{
    public string Name = string.Empty;
    public List<Child> Children = new List<Child>();
}
public class Child
{
    public string Name = string.Empty;
}

1 个解决方案

#1


You can use SelectMany which flattens sub collections :

您可以使用SelectMany来展平子集合:

var all = parentCollection.Select(p=>p.Name)
        .Concat(parentCollection
                    .SelectMany(p=>p.Children).Select(c=>c.Name));

Note this will only work with one depth of parent/child relationship. Yo have true recursion (several levels) you'd have to implement an iterator that yields the children recursively.

请注意,这仅适用于父/子关系的一个深度。哟有真正的递归(几个级别)你必须实现一个递归生成子进程的迭代器。

Edit: to have the children in the correct order, something ugly that works:

编辑:让孩子按照正确的顺序,丑陋的工作:

    var all = parentCollection.Select(p=>new {Parent=p.Name, Name = ""})
            .Concat(parentCollection.SelectMany(p=>p.Children
                            .Select(c => new {Parent=p.Name, c.Name})))
            .OrderBy(o => o.Parent).ThenBy(o => o.Name)
            .Select(o=> o.Name != "" ? o.Name : o.Parent);

#1


You can use SelectMany which flattens sub collections :

您可以使用SelectMany来展平子集合:

var all = parentCollection.Select(p=>p.Name)
        .Concat(parentCollection
                    .SelectMany(p=>p.Children).Select(c=>c.Name));

Note this will only work with one depth of parent/child relationship. Yo have true recursion (several levels) you'd have to implement an iterator that yields the children recursively.

请注意,这仅适用于父/子关系的一个深度。哟有真正的递归(几个级别)你必须实现一个递归生成子进程的迭代器。

Edit: to have the children in the correct order, something ugly that works:

编辑:让孩子按照正确的顺序,丑陋的工作:

    var all = parentCollection.Select(p=>new {Parent=p.Name, Name = ""})
            .Concat(parentCollection.SelectMany(p=>p.Children
                            .Select(c => new {Parent=p.Name, c.Name})))
            .OrderBy(o => o.Parent).ThenBy(o => o.Name)
            .Select(o=> o.Name != "" ? o.Name : o.Parent);