2012-09-07 53 views
6

İçerisinde linq uzmanları için bir sorum var! İç içe geçmiş Bileşen örnekleri listesinde, içinde belirli bir türden bir bileşen olup olmadığını bilmem gerekir. Linq tarafından ifade edilebilir mi? Application.Components [0] .Components [0] .Components [0] ... olabilir. Benim sorum linq yinelemeli sorgular yöneltilmiştir!linq iç içe geçmiş liste

Size model hakkında bazı fikir sahibi olmanız için tarafları bırakıyorum.

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 
} 

cevap

5

yapabilir?

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

neler innercomp.components olacak?

Düzenleme: Yani üst veya ikinci seviyede yinelemeli sadece belirli bir türden bileşenleri bulmak istiyoruz. Bu şekilde

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

kullanılacak:

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

örnek veri:

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

iç komponentler hakkında ne düşünüyorsunuz? –

+0

@IrrationalRationalWorks: Cevabımı düzenledim. –

+0

Ben gökkuşağı yiyiyorum ... –

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

veya

Bir bileşende bileşenlerinden herhangi belirli bir türden olup olmadığını bilmek istiyorum
var listContainsAtLeastOneService = 
    (from c in Components 
     where c.Type == ComponentType.WindowsService 
     select c).Any(); 
+0

neler c.components hakkında Sonra aşağıdaki Traverse uzatma yöntemi kullanabilirsiniz? –

İlgili konular