Enumerating Collections that are not inherently IEnumerable?

后端 未结 5 1601
予麋鹿
予麋鹿 2020-11-29 03:45

When you want to recursively enumerate a hierarchical object, selecting some elements based on some criteria, there are numerous examples of techniques like \"flattening\" a

5条回答
  •  北海茫月
    2020-11-29 04:21

    I guess you are asking for something like this.

    public static IEnumerable  GetNodesRecursively(this TCollection nodeCollection, Func getSub)
     where T, TCollection: IEnumerable
    {   
        foreach (var theNode in )
        {
            yield return theNode;
            foreach (var subNode in GetNodesRecursively(theNode, getSub))
            {
                yield return subNode;
            }
        }
    }
    
    var all_control = GetNodesRecursively(control, c=>c.Controls).ToList();
    

提交回复
热议问题