Search code examples
c#line

LINQ find if nested IEnumerables contain specific values at each level of nested IEnumerable


I need to query items in three levels of nested IEnumerable to find whether I have specific items at each level of the enumeration. I've an IEnumerable of Charts containing Bands and within there Members as below.

Given that I've to check in the IEnumerable of Charts if a chart of a specific name exists, and if so within that specific chart if there's a band with a specific name within the IEnumerable of bands and finally whether within that band there's a member whose name is Z.

Is this possible with a single LINQ query or is there a better way of iterating through those records at each level and confirming that Chart X exists and within Chart X that Band Y exists and within Band Y that member Z exists?

public class chart{
   public string name {get; set;}
   public IEnumerable<Band> Bands {get; set;}
}

public class Band{
   public string name {get; set;}
   public IEnumerable<Member> Members {get; set;}
}

public class Member{
   public string name {get; set;}
   
}



Solution

  • charts
    .Where(c => c.name == "Chart_name")
    .SelectMany(c=> c.Bands)
    .Where(b => b.name == "Band_name")
    .SelectMany(b=> b. Members)
    .Where(m => m.name == "member_name")
    .ToList()