LINQ Select from sub-list
How can I make a Linq query to grab ALL Productpricediscounts 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 has to look something like:
categoryProducts.Select(p => p.productPrices).Select(x => x.?!?!
The problem is that I would have expected the x. - intellisense to suggest priceDiscounts
, but I get "list"-values (like: "Any", 开发者_如何学运维"Select", "Distinct" and so on.)
You'll need to use SelectMany
in order to access priceDiscounts
:
var query = categoryProducts
.SelectMany(x => x.productPrices)
.SelectMany(y => y.priceDiscounts);
You need Enumerable.SelectMany
var result = categoryProducts.SelectMany(x => x.productPrices)
.SelectMany(x => x.priceDiscounts);
精彩评论