I have DB, consists of 2 tables:
**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)
I output the table by pages (5 rows by page). It realize:
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();
But I have unsorted by "salary"-field list.
I tried this code:
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);
but it haven't result.
What did I do wrong?
The OrderBy needs to come before the Skip and Take. Other wise you are just going to order your small result set.
Try:
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With