tags:

views:

177

answers:

2

Hi,

i currently have the problem that i don't know how to make a LINQ select 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;
}

So, my query has to look something like:

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

The problem is that i would hav expected the x. - intellisense to suggest "priceDiscounts", but i get "list"-values ( like: "Any", "Select", "Distinct" and so on..)

Thanks a lot

+1  A: 

You need Enumerable.SelectMany

var result = categoryProducts.SelectMany(x => x.productPrices)
             .SelectMany(x => x.priceDiscounts);
Winston Smith
+2  A: 

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

var query = categoryProducts
            .SelectMany(x => x.productPrices)
            .SelectMany(y => y.priceDiscounts);
LukeH
I think you'll need a second SelectMany instead of a Select, in order to flatten the results.
Winston Smith
@Winston: You're right. That's actually what I wrote originally but then I edited and changed to a `Select`. I've just changed back to `SelectMany`.
LukeH