2014-03-13 147 views
0

我正在一個項目上工作,並且有關於轉換類型的問題。我想爲我的項目創建簡單的搜索,但它一直詢問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); 
} 
+0

在這行你得到這個錯誤?先調試你的代碼。 –

+0

它是在第一個'if'之後:你缺少'.ToPagedList()' – Silvermind

回答

2

雖然products確實IPagedList<Product_List>型的,對IPagedList<Product_List>回報一個IEnumerable<Product_List>實例調用.Where(),它可以」不言而喻地轉換。請注意,你怎麼會在這裏進行轉換:

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); 
相關問題