LINQ Select from sub-list

后端 未结 2 2032
情话喂你
情话喂你 2021-01-06 01:19

How can I make a Linq query to grab ALL Productpricediscounts from a category?

public class ProductCategory
{
    public List categoryProducts         


        
相关标签:
2条回答
  • 2021-01-06 01:35

    You need Enumerable.SelectMany

    var result = categoryProducts.SelectMany(x => x.productPrices)
                 .SelectMany(x => x.priceDiscounts);
    
    0 讨论(0)
  • 2021-01-06 01:36

    You'll need to use SelectMany in order to access priceDiscounts:

    var query = categoryProducts
                .SelectMany(x => x.productPrices)
                .SelectMany(y => y.priceDiscounts);
    
    0 讨论(0)
提交回复
热议问题