ViewResult列表仅显示一个类别

时间:2019-06-07 18:53:47

标签: c# asp.net-core model-view-controller

我想按类别显示项目,但是它仅适用于带有“ 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;
        }

1 个答案:

答案 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); 

请尝试。