c# - linq嵌套列表包含

标签 c# linq recursion tree nested-class

我有一个问题要问你们这些 linq 专家! 在 Component 实例的嵌套列表中,我需要知道其中是否有特定类型的组件。能用linq表达吗?考虑到可能有application.Components[0].Components[0].Components[0]... 我的问题是面向linq中的递归查询!

我将实体留给您,以便您对模型有一些了解。

public class Application
{
    public List<Component> Components { get; set; }
}

public class Component
{
    public ComponentType Type { get; set; }
    public List<Component> Components { get; set; }
}

public enum ComponentType
{
    WindowsService,
    WebApplication,
    WebService,
    ComponentGroup
}

最佳答案

您想知道组件中的任何组件是否属于给定类型?

var webType = ComponentType.WebApplication;
IEnumerable<Component> webApps = from c in components
                                 from innerComp in c.Components
                                 where innerComp.Type == webType;
bool anyWebApp = webApps.Any();

what about innercomp.components?

编辑:因此您希望递归地查找给定类型的组件,而不仅仅是在顶层或二级。然后你可以使用下面的 Traverse 扩展方法:

public static IEnumerable<T> Traverse<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> fnRecurse)
{
    foreach (T item in source)
    {
        yield return item;

        IEnumerable<T> seqRecurse = fnRecurse(item);
        if (seqRecurse != null)
        {
            foreach (T itemRecurse in Traverse(seqRecurse, fnRecurse))
            {
                yield return itemRecurse;
            }
        }
    }
}

以这种方式使用:

var webType = ComponentType.WebApplication;
IEnumerable<Component> webApps = components.Traverse(c => c.Components)
                                 .Where(c => c.Type == webType);
bool anyWebApp = webApps.Any();

示例数据:

var components = new List<Component>() { 
    new Component(){ Type=ComponentType.WebService,Components=null },
    new Component(){ Type=ComponentType.WebService,Components=new List<Component>(){
        new Component(){ Type=ComponentType.WebService,Components=null },
        new Component(){ Type=ComponentType.ComponentGroup,Components=null },
        new Component(){ Type=ComponentType.WindowsService,Components=null },
    } },
    new Component(){ Type=ComponentType.WebService,Components=null },
    new Component(){ Type=ComponentType.WebService,Components=new List<Component>(){
        new Component(){ Type=ComponentType.WebService,Components=new List<Component>(){
            new Component(){Type=ComponentType.WebApplication,Components=null}
        } },
        new Component(){ Type=ComponentType.WindowsService,Components=null },
        new Component(){ Type=ComponentType.WebService,Components=null },
    } },
    new Component(){ Type=ComponentType.WebService,Components=null },
    new Component(){ Type=ComponentType.ComponentGroup,Components=null },
    new Component(){ Type=ComponentType.WebService,Components=null },
};

关于c# - linq嵌套列表包含,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12318372/

相关文章:

java - 即使我认为我已经退出循环,我的递归循环也会出现堆栈溢出错误

c++ - 使用递归 C++ 的最小 int 数组

c# - Caliburn micro ViewModel 没有从另一个 ViewModel 接收消息

C# 和 SQLGeometry : Combining Database Rows and WPF

c# - 使用 WiX C#/.NET 4 自定义操作时出现错误 2896

c# - 从 Ilist<> 到 List<> 的 Linq 查询转换

c# - 如何使用单个 LINQ 查询按年份对事件进行分组?

c# - 是否可以使用 Linq 比较同一 IEnumerable 中的两个字段?

c++ - 如何使用迭代器在 C++ 中的递归函数中传递值?

c# 相当于 clojure 中的 base64 + UrlTokenEncode