2012-09-07 4 views
6

나는 당신을 위해 linq 전문가를위한 질문이 있습니다! Component 인스턴스의 중첩 목록에서 특정 유형의 구성 요소가 있는지 알아야합니다. 그것은 linq에 의해 표현 될 수 있습니까? 고려해야 할 application.Components [0] .Components [0] .Components [0] ... 내 질문은 linq에 재귀 쿼리 지향입니다!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 
} 

답변

5

을 할 수 있습니까?

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 어떻습니까?

편집 : 그래서 당신은 상단 또는 두 번째 수준에 재귀 적으로뿐만 아니라 특정 유형의 구성 요소를 찾고 싶어요. 이 방법으로

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

innercomp.components는 어떻습니까? –

+0

@IrrationalRationalWorks : 내 대답을 수정했습니다. –

+0

나는 무지개를 먹고있다. 내가 체크하자. –

1
if(Components.Any(c => c.Type == ComponentType.WindowsService)) 
{ 
    // do something 
} 

하거나

당신은 구성 요소의 구성 요소가 지정된 유형의 경우 알고 싶어
var listContainsAtLeastOneService = 
    (from c in Components 
     where c.Type == ComponentType.WindowsService 
     select c).Any(); 
+0

무엇 c.components에 대한 다음 당신은 다음과 같은 Traverse 확장 방법을 사용할 수 있습니까? –