linq嵌套列表包含 [英] linq nested list contains

查看:76
本文介绍了linq嵌套列表包含的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向您询问linq专家! 在Component实例的嵌套列表中,我需要知道其中是否存在特定类型的组件.可以用linq表示吗?考虑到可能存在application.Components [0] .Components [0] .Components [0] ...我的问题是针对linq中的递归查询!

I have a question for you linq experts out there! In a nested list of Component instances, I need to know if there is a component of a particular type in it. Can it be expressed by linq? Take into account that there may be application.Components[0].Components[0].Components[0]... My question is oriented to recursive queries in linq!

我将实体留给您,让您对模型有所了解.

I leave you the entities for you to have some idea of the model.

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
}

推荐答案

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

You want to know if any of the components in a component is of a given type?

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


innercomp.components呢?

what about innercomp.components?

编辑:因此,您不仅要在顶层或第二层上递归查找给定类型的组件.然后,您可以使用以下Traverse扩展方法:

Edit: So you want to find components of a given type recursively not only on the top or second level. Then you can use following Traverse extension method:

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 },
};

这篇关于linq嵌套列表包含的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆