如何使用LINQ选择复合对象的所有后代

sma*_*man 5 c# linq linq-to-objects composite .net-3.5

如何ComponentTraversal.GetDescendants()更好地使用LINQ?

public static class ComponentTraversal
{
    public static IEnumerable<Component> GetDescendants(this Composite composite)
    {
        //How can I do this better using LINQ?
        IList<Component> descendants = new Component[]{};
        foreach(var child in composite.Children)
        {
            descendants.Add(child);
            if(child is Composite)
            {
                descendants.AddRange((child as Composite).GetDescendants());
            }
        }
        return descendants;
    }
}
public class Component
{
    public string Name { get; set; }
}
public class Composite: Component
{
    public IEnumerable<Component> Children { get; set; }
}
public class Leaf: Component
{
    public object Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

回答

我编辑了Chris的答案,提供了我添加到Common库中的通用扩展方法.我可以看到这对其他人也有帮助,所以这里是:

    public static IEnumerable<T> GetDescendants<T>(this T component, Func<T,bool> isComposite, Func<T,IEnumerable<T>> getCompositeChildren)
    {
        var children = getCompositeChildren(component);
        return children
            .Where(isComposite)
            .SelectMany(x => x.GetDescendants(isComposite, getCompositeChildren))
            .Concat(children);
    }
Run Code Online (Sandbox Code Playgroud)

谢谢克里斯!

也,

请访问http://blogs.msdn.com/b/wesdyer/archive/2007/03/23/all-about-iterators.aspx查看LukeH的答案.他的回答提供了更好的方法来解决这个问题,但我没有选择它,因为它不是我问题的直接答案.

Luk*_*keH 7

通常有很好的理由避免(1)递归方法调用,(2)嵌套迭代器,以及(3)大量的一次性分配.这种方法避免了所有这些潜在的陷阱:

public static IEnumerable<Component> GetDescendants(this Composite composite)
{
    var stack = new Stack<Component>();
    do
    {
        if (composite != null)
        {
            // this will currently yield the children in reverse order
            // use "composite.Children.Reverse()" to maintain original order
            foreach (var child in composite.Children)
            {
                stack.Push(child);
            }
        }

        if (stack.Count == 0)
            break;

        Component component = stack.Pop();
        yield return component;

        composite = component as Composite;
    } while (true);
}
Run Code Online (Sandbox Code Playgroud)

这是通用的等价物:

public static IEnumerable<T> GetDescendants<T>(this T component,
    Func<T, bool> hasChildren, Func<T, IEnumerable<T>> getChildren)
{
    var stack = new Stack<T>();
    do
    {
        if (hasChildren(component))
        {
            // this will currently yield the children in reverse order
            // use "composite.Children.Reverse()" to maintain original order
            // or let the "getChildren" delegate handle the ordering
            foreach (var child in getChildren(component))
            {
                stack.Push(child);
            }
        }

        if (stack.Count == 0)
            break;

        component = stack.Pop();
        yield return component;
    } while (true);
}
Run Code Online (Sandbox Code Playgroud)

  • 我将使用你的扩展方法与IEnumerable <T>实现,但它的缺点是它与IQueryable <T>不兼容(这是弯曲内置LINQ方法有时有意义的原因 - 兼容与现有的LINQ提供商). (2认同)