LINQ: How to get items from an inner list into one list?
c#, linq, list
Solution
Scott's answer is great; I'd just like to point out that you can in fact do this query using query continuation syntax:
from parent in parents
where parent.Children.Any (c => c.CategoryNumber==1)
select parent into p
from child in p.Children
where child.CategoryNumber == 2
select child
Notice how the "into" lets you pipe the result of one query into the next query. Pretty slick, eh?
Problem
Having the following classes (highly simplified): ``` public class Child { public string Label; public int CategoryNumber; public int StorageId; } public class Parent { public string Label; public List<Child> Children = new List<Child>(); } ``` And having the following data: ``` var parents = new List<Parent>(); var parent = new Parent() {Label="P1"}; parent.Children.Add(new Child() {Label="C1", CategoryNumber=1, StorageId=10}); parent.Children.Add(new Child() {Label="C2", CategoryNumber=2, StorageId=20}); parents.Add(parent); parent = new Parent() {Label="P2"}; parent.Children.Add(new Child() {Label="C3", CategoryNumber=1, StorageId=10}); parent.Children.Add(new Child() {Label="C4", CategoryNumber=2, StorageId=30}); parents.Add(parent); parent = new Parent() {Label="P3"}; parent.Children.Add(new Child() {Label="C5", CategoryNumber=3, StorageId=10}); parent.Children.Add(new Child() {Label="C6", CategoryNumber=2, StorageId=40}); parents.Add(parent); ``` Now, how would I get a list of children (with CategoryNumber=2) from the list of parents containing at least one child with CategoryNumber = 1 ? I can do the following but it does not appear to be optimal: ``` var validParents = from p in parents where p.Children.Any (c => c.CategoryNumber==1) select p; var selectedChildren = validParents.Select(p => from c in p.Children where c.CategoryNumber == 2 select c); ``` Here's what I get for selectedChildren: - `IEnumerable<IEnumerable<Child>>` - `IEnumerable<Child>` - C2 2 20 - `IEnumerable<Child>` - C4 2 30 Is it possible to only have one flat list containing the two children elements instead of two sub-list? How would it translate in LINQ ?