我正在一個項目上工作,並且有關於轉換類型的問題。我想爲我的項目創建簡單的搜索,但它一直詢問Ienumerable
類型,這實際上我不明白。請幫忙。錯誤:「不能隱式轉換類型」
問題:
Error 1 Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'PagedList.IPagedList'. An explicit conversion exists (are you missing a cast?) 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);
}
在這行你得到這個錯誤?先調試你的代碼。 –
它是在第一個'if'之後:你缺少'.ToPagedList()' – Silvermind