我想按类别显示项目,但是它仅适用于带有“ Fresh”字符串的项目。 “ Not-fresh”字符串不返回任何内容
public ViewResult List(string category)
{
string _category = category;
IEnumerable<Product> products;
string currentCategory = string.Empty;
if (string.IsNullOrEmpty(category))
{
products = _productRepository.Products.OrderBy(p => p.ProductId);
currentCategory = "All products";
}
else
{
if (string.Equals("Fresh", _category, StringComparison.OrdinalIgnoreCase))
{
products = _productRepository.Products.Where(p => p.Category.CategoryName.Equals("Fresh")).OrderBy(p => p.Name);
}
else
{
products = _productRepository.Products.Where(p => p.Category.CategoryName.Equals("Not-fresh")).OrderBy(p => p.Name);
}
currentCategory = _category;
}
答案 0 :(得分:1)
string.Equals(“ Fresh”,_category,StringComparison.OrdinalIgnoreCase)
F-f
products = _productRepository.Products.Where(p => p.Category.CategoryName.Equals("Not-Fresh")).OrderBy(p => p.Name);
请尝试。