开发者

Using LINQ to query collections within collections in am MVC View

开发者 https://www.devze.com 2023-01-20 06:22 出处:网络
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 g

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


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

0

精彩评论

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