views:

55

answers:

1

I have a "Product" and a "Benefit" class which is shown below:

Now, I want to query a collection of products from within my view. I've created a LINQ query that gets the products but I want to get at the collection of benefits that belong within the product.

Code within my view (however this just iterates each product and I need to iterate the Benfits within the Product):

<% foreach (var benefit in Model.Products.Where(x => x.ProductId == "123"))
    { %>

Classes:

public class Product
{
    public string ProductId { get; set; }
    public string ProductName { get; set; }
    public string Description { get; set; }
    public string Features { get; set; }
    public List<Benefit> Benefits { get; set; }
    public decimal Price { get; set; }
}

public class Benefit
{
    public string Name { get; set; }
    public string Value { get; set; }
}
+2  A: 

Model.Products.Where(x => x.ProductId == "123").SelectMany(p=>p.Benefits)

CodeInChaos
Brilliant. I love LINQ! Thanks CinC!
StephenLewes
I just want to add slightly to this question...
StephenLewes