I want to find all Persons who are left handed using recursive linq via extension method.
I've seen this answer but there is a problem (imho) with the this
: (when applied as extension method because of the static context)
keyword this is not valid in static method
So here is what i've tried :
I have a Person
class :
public class Person
{
public List<Person> Children = new List<Person>();
public bool IsLeftHanded;
}
And here is the code for the externsion method :
public static class Extensions
{
public static IEnumerable<Person> DescendantsAndSelf(this IEnumerable<Person> list)
{
yield return this;
foreach (var item in list.SelectMany(x => x.Children.DescendantsAndSelf()))
{
yield return item;
}
}
}
But there is a problem with yield return this;
Question :
How can I fix
my code in order to support the "me and my childs" trick ? ( goal : find all persons who are left handed)
nb
please notice that I want to use linq and recursion in order to get experienced with linq using recursion.