Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 递归linq和这个?_C#_Linq_.net 4.0 - Fatal编程技术网

C# 递归linq和这个?

C# 递归linq和这个?,c#,linq,.net-4.0,C#,Linq,.net 4.0,我想通过扩展方法使用递归linq找到所有左撇子 我已经看到了这一点,但this有一个问题(imho):(由于静态上下文,当作为扩展方法应用时) 关键字这在静态方法中无效 以下是我尝试过的: 我有一个人班: public class Person { public List<Person> Children = new List<Person>(); public bool IsLeftHanded; } 公共类人物 { public L

我想通过扩展方法使用递归linq找到所有左撇子

我已经看到了这一点,但
this
有一个问题(imho):(由于静态上下文,当作为扩展方法应用时)

关键字这在静态方法中无效

以下是我尝试过的:

我有一个
班:

public class Person
{
        public  List<Person> Children = new List<Person>();
        public bool IsLeftHanded;
}
公共类人物
{
public List Children=new List();
公共场所被关闭;
}
下面是extension方法的代码:

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;
        }
    }
}
公共静态类扩展
{   
公共静态IEnumerable子代和自身(此IEnumerable列表)
{
收益回报这一点;
foreach(列表中的var项。SelectMany(x=>x.Children.degenantsandself())
{
收益回报项目;
}
}
}
但是
收益率在这方面存在问题

问题:

我怎样才能
修复我的代码以支持“我和我的孩子”的把戏?(目标:找到所有左撇子)


请注意,我想使用linq和递归,以便体验使用递归的linq。

我希望看到它像这样工作:

public static IEnumerable<Person> DescendantsAndSelf(this Person person)
{
    yield return person;
    foreach (var item in person.Children.SelectMany(x => x.DescendantsAndSelf()))
    {
        yield return item;
    }
}
var person = new Person();
... do stuff
var desc = person.DescendantsAndSelf();

如果我看错了,请纠正我。

这是一个更通用的版本,适用于任何集合

public static IEnumerable<T> SelectDescendents<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> selector)
{
        foreach(var item in source)
        {
            yield return item;
            foreach(T item2 in SelectDescendents(selector(item), selector))
                yield return item2;
        }
}

在静态类中不起作用。您应该传递一个Person类。@JeroenvanLangen是的,正如您所看到的,我已经写过,由于静态上下文,它无法工作。我认为wudzik的解决方案非常合适。感谢您传递一个
IEnumerable
,您应该传递“root”人员。(就像乌兹克做的+1)@RoyiNamir有什么答案对你有用吗?你需要更好的答案吗?愚蠢的问题,但使用“收益回报”(而不是建立一个列表并返回它)的目的仅仅是为了获得延迟执行的好处,还是还有其他原因呢?@vargonian延迟执行是原因,请阅读以下帖子:非常感谢,这正是我所需要的。这将如何回报这个人呢?看看我的结构please@RoyiNamir在您的情况下,因为您没有人员集合,所以它将是:
new[]{Person}。选择后代(p=>p.Children)
Persons.SelectDescendents(p => p.Children);