Mameko Mikhail
Mameko Mikhail

Reputation: 51

Don't order by attribute

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?

Upvotes: 3

Views: 88

Answers (1)

brimble2010
brimble2010

Reputation: 18374

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();

Upvotes: 3

Related Questions