无法隐式转换类型,存在显式转换(您是否缺少类型转换?)

时间:2019-02-03 07:31:19

标签: c# visual-studio linq

我已经建立了一个ViewModel(以前使用过ViewBag),但是在以下代码中遇到了错误,我已经对其进行了研究,但亲自无法解决我的问题:

public ActionResult Index(string category, string search)
    {
        ProductIndexViewModel viewModel = new ProductIndexViewModel();
        var products = db.Products.Include(p => p.Category);

        if (!String.IsNullOrEmpty(search))
        {
            products = products.Where(p => p.Name.Contains(search) ||
                    p.Description.Contains(search) ||
                    p.Category.Name.Contains(search));

            viewModel.Search = search;

        }

        viewModel.CatsWithCount = from matchingProducts in products
                                  where matchingProducts.CategoryID != null
                                  group matchingProducts by
                                  matchingProducts.Category.Name into
                                  catGroup
                                  select new CategoryWithCount()
                                  {
                                      CategoryName = catGroup.Key,
                                      ProductCount = catGroup.Count()
                                  };

        if (!String.IsNullOrEmpty(category))
        {
           products = products.Where(p => p.Category.Name == category);
        }

        viewModel.Products = products;
        return View(viewModel);
    }

该错误发生在该行的底部:

viewModel.Products = products;

确切的错误是

  

“无法隐式转换类型   'System.Linq.IQueryable<OnlineStore.Models.Product>'至   'System.Linq.IQueryable<OnlineStore.ViewModels.ProductIndexViewModel>'。   存在显式转换(您是否缺少演员表?)”

我对使用Visual Studio还是很陌生,只是想知道要解决此错误我该怎么做。

编辑:

ProductIndexViewModel:

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace OnlineStore.ViewModels
{
    public class ProductIndexViewModel
    {
        public IQueryable<ProductIndexViewModel> Products { get; set; }
        public string Search { get; set; }
        public IEnumerable<CategoryWithCount> CatsWithCount { get; set; }
        public string Category { get; set; }
        public IEnumerable<SelectListItem> CatFilterItems
        {
            get
            {
                var allCats = CatsWithCount.Select(cc => new SelectListItem
                {
                    Value = cc.CategoryName,
                    Text = cc.CatNameWithCount
                });
                return allCats;
            }
        }
    }
    public class CategoryWithCount
    {
        public int ProductCount { get; set; }
        public string CategoryName { get; set; }
        public string CatNameWithCount
        {
            get
            {
                return CategoryName + " (" + ProductCount.ToString() + ")";
            }
        }
    }
}````

2 个答案:

答案 0 :(得分:1)

问题出在我的ProductIndexViewModel类中。我有

public IEnumerable<ProductIndexViewModel> Products { get; set; } 

代替

public IEnumerable<Product> Products { get; set; }

感谢所有回答的人。

答案 1 :(得分:0)

您的ProductsProductIndexViewModel类中是错误的

public class ProductIndexViewModel
{
    public IQueryable<ProductIndexViewModel> Products { get; set; }

  1. Products是一个IQueryable,实际上没有意义,可能应该是List<T>IEnumerable<T>

示例

public List<ProductIndexViewModel> Products { get; set; }

  1. 通用参数看起来不正确,对于ViewModel,它可能应该为Product或某些ProductViewModel之类的ViewModel,您需要Select /投影到它

  1. 您可能应该在结果中致电ToList

示例

viewModel.Products = products.ToList();