这是我的简单清单:
public class ProductStore
{
public List<Product> AllProducts
{
get
{
return new List<Product>
{
new Product{Name = "Stove 1", Category= "Stoves", ID = 1, Price = 99.99},
new Product{Name = "Stove 2", Category= "Fireplaces", ID = 2, Price = 139.50},
new Product{Name = "Stove 3", Category= "Stoves", ID = 3, Price = 199.99},
new Product{Name = "Stove 4", Category= "Stoves", ID = 4, Price = 29.00},
};
}
}
}
这就是我在视图中打印这些数据的方式:@model List
@{
ViewBag.Title = "AllProducts";
}
<h2>AllProducts</h2>
<ul>
@foreach (var product in Model)
{
<li>Name: @product.Name, Category:@product.Category, Price:@product.Price;</li>
}
</ul>
我的问题是:什么是只打印那些元素的最佳方法,即。类别 == 炉子?我知道我可以在 foreach 中将 if 语句与 continue 结合使用,但我想知道,有没有更聪明的方法来做到这一点?