简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM