LINQ Select from the sub-list

How can I make a Linq request to grab ALL trademarks from a category?

public class ProductCategory { public List<Product> categoryProducts; } public class Product { public List<Productprice> productPrices; } public class Productprice { public List<Productpricediscount> priceDiscounts; } 

My query should look something like this:

 categoryProducts.Select(p => p.productPrices).Select(x => x.?!?! 

The problem is that I would expect x. - intellisense offer priceDiscounts , but I get a "list" of values ​​(for example: "Any", "Select", "Miscellaneous", etc.)

+4
source share
2 answers

You will need to use SelectMany to access priceDiscounts :

 var query = categoryProducts .SelectMany(x => x.productPrices) .SelectMany(y => y.priceDiscounts); 
+12
source

You need Enumerable.SelectMany

 var result = categoryProducts.SelectMany(x => x.productPrices) .SelectMany(x => x.priceDiscounts); 
+4
source

All Articles