LINQ to get all the children of the heirs

I dig for a long time.

public class Person
{
   public string Name { get; set; }    
   public string Age { get; set; }    
   public List<Person> Children { get; set; }    
}

I want one LINQ query to find "All the faces whose Age > 4in this collection."

Note. You must go through Collection of Person+ Collection Children, so each child will have a collection Personuntil Childrenit becomes null.

+4
source share
3 answers

At first I do not understand why all of your property privateand Ageare not int. So my class is as follows:

public partial class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public List<Person> Childrens { get; set; }
}

partial . , - .

:

public partial class Person
{
    public Person GetPersonWithChindren(int maxAge)
    {
        return new Person
        {
            Age = this.Age,
            Name = this.Name,
            Childrens = this.Childrens != null 
            ? this.Childrens
                .Where(x => x.Age < maxAge)
                .Select(x => x.GetPersonWithChindren(maxAge)) //this line do recursive magic
                .ToList() 
            : null
        };
    }
}

, Age Age , Childrens null.

, :

var person = new Person()
{
  //initialisation of your collection here
}

//result will contains only nodes where Person have age < 4 and Childs that have age < 4
var result = person.GetPersonWithChindren(4);

, linqToEntities. LinqToSQL, Person. , , . CTE LinQ.

UPDATE:

Func<T> :

public partial class Person
{
    public Person GetPersonWithChindren(Func<Person, bool> func)
    {
        return new Person
        {
            Age = this.Age,
            Name = this.Name,
            Childrens = this.Childrens != null
            ? this.Childrens
                .Where(x => func(x))
                .Select(x => x.GetPersonWithChindren(func))
                .ToList()
            : null
        };
    }
}

:

var result = person.GetPersonWithChindren(x => x.Age < 4);

, .

+3

. , :

public static class Helpers
public static IEnumerable<Person> GetDescendants(this Person person)
{
    foreach (var child in person.Children)
    {
        yield return child;
        foreach (var descendant in child.GetDescendants())
        {
           yield return descendant;
        }
    }
}

, .

+3

, .Children , :

Func<Person, Func<Person, bool>, Person> clone = null;
clone = (p, f) => f(p) ? new Person()
{
    Name = p.Name,
    Age = p.Age,
    Children = p.Children.Select(c => clone(c, f)).Where(x => x != null).ToList(),
} : null;

var olderThan4 = clone(person, p => p.Age > 4);

, . .

:

var person = new Person()
{
    Name = "Fred", Age = 30,
    Children = new List<Person>()
    {
        new Person() { Name = "Bob", Age = 7, },
        new Person() { Name = "Sally", Age = 3, }
    },
};

... then you get this result:

result

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    private List<Person> _children = null;
    public List<Person> Children
    {
        get
        {
            if (_children == null)
            {
                _children = new List<Person>();
            }
            return _children;
        }
        set
        {
            _children = value;
        }
    }
}
+1
source

Source: https://habr.com/ru/post/1626814/


All Articles