2015-08-19 36 views
-1

之前調用我有以下代碼。我沒有使用過濾或搜索信息的排序。我也沒有使用skip方法。請參閱下面的代碼。分頁錯誤:「跳過」方法僅支持LINQ to Entities中的排序輸入。方法'OrderBy'必須在方法'Skip'

public ActionResult Index(string currentFilter=null, string search=null, string searchBy=null, int? page=1) 
    { 
     var student = from d in db.Student_vw 
         where d.is_active == true 
         select d; 
     if (searchBy == "default") 
     { 
      student = student.OrderByDescending(x => x.ID_Number); 
     } 

     //searching of an item 
     if (!String.IsNullOrEmpty(search)) 
     { 
      student = student.Where(x => x.ID_Number.Contains(search) || x.student_fname.Contains(search) 
       || x.student_lname.Contains(search) || x.section_name.Contains(search) || x.course_name.Contains(search) 
       || x.student_address.Contains(search) || x.batch_name.Contains(search) || x.adviser_fname.Contains(search) || x.adviser_lname.Contains(search) || x.student_email_add.Contains(search)); 
     } 
     else { 
      student = student.OrderByDescending(x => x.ID_Number); 
     } 
     ViewBag.CurrentFilter = search; 

     int pageSize = 25; 
     int pageNumber = (page ?? 1); 

     var returnMe = student.ToPagedList(pageNumber, pageSize); 

     return View(returnMe); 
    } 

在此先感謝您的幫助。

+2

ToPagedList使用跳過,您需要先排序您的結果,如異常消息所述。 –

回答

0

@ Jen-您需要的時候具有價值,而不是有序的搜索學生student.ToPageList(PAGENUMBER,pageSize的)

1
在你的代碼

,之前使用排序依據。做如下

public ActionResult Index(string currentFilter=null, string search=null, string searchBy=null, int? page=1) 
    { 
     var student = from d in db.Student_vw 
         where d.is_active == true 
         select d; 
     //if (searchBy == "default") 
     //{ 
     // student = student.OrderByDescending(x => x.ID_Number); 
     //} 

     //searching of an item 
     if (!String.IsNullOrEmpty(search)) 
     { 
      student = student.Where(x => x.ID_Number.Contains(search) || x.student_fname.Contains(search) 
       || x.student_lname.Contains(search) || x.section_name.Contains(search) || x.course_name.Contains(search) 
       || x.student_address.Contains(search) || x.batch_name.Contains(search) || x.adviser_fname.Contains(search) || x.adviser_lname.Contains(search) || x.student_email_add.Contains(search)); 
     } 
     //else { 
     // student = student.OrderByDescending(x => x.ID_Number); 
     //} 
     ViewBag.CurrentFilter = search; 

     int pageSize = 25; 
     int pageNumber = (page ?? 1); 

     var returnMe = student.OrderByDescending(x => x.ID_Number).ToPagedList(pageNumber, pageSize); 

     return View(returnMe); 
    } 
0

,除非你給它通過一個訂單,因此,使用以跳過或(其中PagedList內部所做的),而不使用ORDER BY沒有任何邏輯意義SQL不保證結果的順序和理論上可以每次給你完全不同的結果。

您編寫自己的邏輯的方式可以在沒有每次按語句順序執行的情況下完成。

相關問題