我有DB,由2个表组成:
**Job**
job_id (int, primary key),
job_nm (nchar(50))
**Employee**
empl_id (int, primary key)
job_id (int, forein key) (one-to-many)
first_name (nchar(50))
last_name (nchar(50))
salary (float)
我按页输出表(每页5行)。它意识到:
ViewBag.Tab = (from employee in db.Employees
join job in db.Jobs on employee.job_id equals job.job_id
where job.job_nm == sel1
orderby employee.salary
select employee
).Skip(pageSize * pageNum)
.Take(pageSize)
.ToList();
但是我没有按"薪水"字段排序。
我试过这个代码:
ViewBag.Tab = (from employee in db.Employees
join job in db.Jobs
on employee.job_id equals job.job_id
where job.job_nm == sel1
orderby employee.salary
select employee
).Skip(pageSize * pageNum)
.Take(pageSize)
.ToList()
.OrderBy(s => s.salary);
但没有结果。
我做错了什么?
OrderBy
需要在Skip
和Take
之前。否则,你就只能对你的小结果集进行排序。
试题:
ViewBag.Tab = (from employee in db.Employees
join job in db.Jobs
on employee.job_id equals job.job_id
where job.job_nm == sel1
orderby employee.salary
select employee
).OrderBy(s => s.salary)
.Skip(pageSize * pageNum)
.Take(pageSize)
.ToList();