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))
.ToList()
: null
};
}
}
, Age Age , Childrens null.
, :
var person = new Person()
{
}
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);
, .