开发者

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜