简体   繁体   中英

problems with Linq skip take

i have this code..

        var documents = from d in db.Documents select d;
        documents = documents.OrderBy(d => d.Created);
        documents = documents.Skip(20).Take(10);

In db i have 25 documents (ids from 1 to 25). When i run this code i get documents from id 19 to 24.

If i write it like this

    documents = documents.Skip(20);

or like this

documents = documents.Skip(20).ToList().Take(10).AsQueryable();

I get documents from id 20 to 25..

What i am missing here??

Your data is not what you think. The problem is the created date in id=25 is not in order.

You are ordering your sequence by created first, so why would you expect the output to be ordered (or the selection based on) by id ?

Doing the Skip first and the OrderBy second changed the output, because it was probably ordered by Id initially, then you took the last few in the sequence, then ordered them by date again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM