简体   繁体   English

Linq跳过问题

[英]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). 在数据库中,我有25个文档(标识从1到25)。 When i run this code i get documents from id 19 to 24. 当我运行此代码时,我会得到ID为19到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.. 我收到的文件从20到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. 问题是id=25created日期不正确。

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 ? 您要先按created顺序对序列进行排序,那么为什么还要期望对输出按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. 首先执行“ Skip ,然后第二次“ OrderBy更改输出,因为它最初可能是由Id排序的,然后您选择了序列中的最后几个,然后再次按日期对其进行了排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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