IQueryable ToList() seems to lose the ordering
I've an EF4 model and a method to get paginated items (Anagrafica entity) from the model, something like (simplyfied):
public List<Anagrafica> Get(string SortExpression, int startRowIndex, int maximumRows)
{
return context.Anagrafiche.Where(/* some filters */)
.OrderBy(SortExpression)
.Skip(startRowIndex)
.Take(maximumRows).ToList();
}
It's all managed with an ObjectDataSource in an Asp.Net project, and the data are binded to a Telerik RadGrid.
The problem is that, if I set the SortExpression to a field with null values, for some reason the List given by the method seem开发者_运维百科s to have a random order.
It is the correct "page" of results (for example the same first 10 elements I can get from a sql query), but internally to the List the order is randomized.
And I don't know WHY.
I guess the problem lies on skip function.
We have two iqueryable list which are concatinated into one and these two queries are ordered themselves. If we get the result without skip function it is all ok but after using skip order becomes ruined. I need to fix this problem but right now it stays like a nightmare.
I mean like this:
IQueryable list 1: 1. ID:1 2. ID:5 3. ID:8
IQueryable list 2: 1. ID:2 2. ID:3 3. ID:9
CocatList = list1.concat(list2)
if we take without using skip function the result is:
- ID:1
- ID:5
- ID:8
- ID:2
- ID:3
- ID:9
but if we use skip the result is like:
Page 1 (without skip, with take): 1. ID:1 2. ID:5 3. ID:8
Page 2 (with skip, with take): 1. ID:3 2. ID:9 3. ID:5
It is all caused by skip I guess.
精彩评论