不按属性排序

时间:2014-03-14 09:38:09

标签: c# linq

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

但是我没有通过" salary" -field list。

我试过这段代码:

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

但它没有结果。

我做错了什么?

1 个答案:

答案 0 :(得分:3)

OrderBy需要先于SkipTake。另外,你只是要订购你的小结果集。

尝试:

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