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();
what about innercomp.components?
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;
}
}
}
}
to be used in this way:
var webType = ComponentType.WebApplication;
IEnumerable<Component> webApps = components.Traverse(c => c.Components)
.Where(c => c.Type == webType);
bool anyWebApp = webApps.Any();
sample data:
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 },
};