Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't order by attribute

Tags:

c#

linq

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?

like image 371
Mameko Mikhail Avatar asked Dec 07 '25 11:12

Mameko Mikhail


1 Answers

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();
like image 71
brimble2010 Avatar answered Dec 09 '25 00:12

brimble2010



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!