错误:"无法隐式转换类型"

时间:2014-03-13 14:58:51

标签: c# html asp.net css asp.net-mvc

我正在处理一个项目,我对转换类型有疑问。我想为我的项目创建简单的搜索,但它一直在询问Ienumerable类型,老实说我不明白。请帮忙。

问题:

  

错误1无法隐式转换类型' System.Collections.Generic.IEnumerable'到#d; PagedList.IPagedList'。存在显式转换(您是否缺少演员?)C:\ Users \ Asus \ Documents \ Visual Studio 2012 \ Projects \ Nordstromapp \ Nordstromapp \ Controllers \ ProductController.cs 38 24 Nordstromapp

我的代码:

{
    NordstromEntities _db;

    public ProductController()
    {
        _db = new NordstromEntities();
    }
    //
    // GET: /Product/

    public ActionResult Index(string sortOrder, string CurrentSort, int? page, string searchString)
    {
        int pageSize = 10;
    int pageIndex = 1;
    pageIndex = page.HasValue ? Convert.ToInt32(page) : 1;

    ViewBag.CurrentSort = sortOrder;

    sortOrder = String.IsNullOrEmpty(sortOrder) ? "ID" : sortOrder;

    IPagedList<Product_list> products = null;



    if (!String.IsNullOrEmpty(searchString))
    {
        products = products.Where(m => m.ID.ToUpper().Contains(searchString.ToUpper())
                               || m.Product_name.ToUpper().Contains(searchString.ToUpper()));
    }

    switch (sortOrder)
    {
        case "ID":
            if(sortOrder.Equals(CurrentSort))  
                products = _db.Product_list.OrderByDescending
                        (m => m.ID).ToPagedList(pageIndex, pageSize);   
            else
                products = _db.Product_list.OrderBy
                        (m => m.ID).ToPagedList(pageIndex, pageSize);   
            break;

        case "Product_name":
            if (sortOrder.Equals(CurrentSort))
                products = _db.Product_list.OrderByDescending
                        (m => m.Product_name).ToPagedList(pageIndex, pageSize);
            else
                products = _db.Product_list.OrderBy
                        (m => m.Product_name).ToPagedList(pageIndex, pageSize);
            break;

        case "Product_category":
            if (sortOrder.Equals(CurrentSort))
                products = _db.Product_list.OrderByDescending
                        (m => m.Product_category).ToPagedList(pageIndex, pageSize);
            else
                products = _db.Product_list.OrderBy
                        (m => m.Product_category).ToPagedList(pageIndex, pageSize);
            break;

        case "Price":
            if (sortOrder.Equals(CurrentSort))
                products = _db.Product_list.OrderByDescending
                        (m => m.Price).ToPagedList(pageIndex, pageSize);
            else
                products = _db.Product_list.OrderBy
                        (m => m.Price).ToPagedList(pageIndex, pageSize);
            break;


    }
    return View(products);
}

1 个答案:

答案 0 :(得分:2)

虽然products确实属于IPagedList<Product_List>类型,但.Where() 实例上对IPagedList<Product_List>的调用会返回 IEnumerable<Product_List> ,不能隐式转换。请注意您在此处执行转换的方式:

products = _db.Product_list.OrderByDescending(m => m.ID)
                           .ToPagedList(pageIndex, pageSize);

您只需在.Where()电话后执行相同的转化:

products = products.Where(m => m.ID.ToUpper().Contains(searchString.ToUpper()) ||
                               m.Product_name.ToUpper().Contains(searchString.ToUpper()))
                   .ToPagedList(pageIndex, pageSize);