分页错误:方法'跳过'仅支持LINQ to Entities中的排序输入。方法' OrderBy'必须在方法之前调用'跳过'

时间:2015-08-19 08:07:35

标签: c# asp.net-mvc

我有以下代码。我没有使用过滤或搜索信息进行排序。我也没有使用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);
    }

提前感谢您的帮助。

3 个答案:

答案 0 :(得分: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);
    }

答案 1 :(得分:0)

@ Jen-你需要在student.ToPageList(pageNumber,pageSize)之前使用OrderBy

答案 2 :(得分:0)

SQL不保证结果的顺序,除非你给它一个顺序,所以使用Take或Skip(PagedList在内部执行)没有顺序,没有任何逻辑意义,理论上可以每次给你完全不同的结果。

你编写逻辑的方式可以在没有按语句命令的情况下完成。