When I am using Entity Framework with
OrderBy
Skip
Take
jobs = context.Jobs.Include("Company").
OrderBy(x => x.Company.Name).
Skip((page - 1) * PageSize).
Take(PageSize).ToList();
Job
x => x.Title
Job
Company
Your query doesn't completely define the order of resulting rows.
Say we have a table:
Id Name
1 Bar
2 Foo
3 Bar
When ordering by Name
, the following result sets will be possible:
Id Name
1 Bar
3 Bar
2 Foo
and
Id Name
3 Bar
1 Bar
2 Foo
Each call may return any of this sets, so if we have page size 1
it is possible to get strange result like this:
Id Name
1 Bar // 1st set
1 Bar // 2nd set
2 Foo // 1st set
Adding some unique attribute (PK in most cases) to your key will fix this behavior.
Given key Name, Id
there's only one possible result:
Id Name
1 Bar
3 Bar
2 Foo
So your query should look like this (assuming JobId
is PK):
jobs = context.Jobs.Include("Company").
OrderBy(x => x.Company.Name).
ThenBy(x => x.JobId).
Skip((page - 1) * PageSize).
Take(PageSize).ToList();