简体   繁体   English

使用LINQ进行分页而不进行排序

[英]Paging with LINQ without sorting

I want to create paging with LINQ to entity (EF) ,I found Skip method but it is only supported for sorted input,so I think it will be slow in large amount of rows. 我想用LINQ to entity(EF)创建分页,我发现Skip方法但它只支持排序输入,所以我认为它在大量行中会很慢。 Is any way to paging without sorting it first? 有没有办法分页而不先排序?

A database is not obliged to return the set of records in same order, for a specific SQL query, without a specific order declared. 对于特定的SQL查询,数据库没有义务以相同的顺序返回记录集,而不声明特定的顺序。

You may run the same query and first 10 records be different on each run. 您可以运行相同的查询,并且每次运行前10个记录都不同。 Database will execute this based on what is best for itself (what part of data is cached in memory, what parts changed before two executions of the same query and the like). 数据库将根据最适合自己的内容执行此操作(数据的哪一部分缓存在内存中,哪些部分在两次执行同​​一查询之前发生了变化等)。

So regardless of what ORM you use, you have to specify in what order you want the data. 因此,无论您使用什么ORM,都必须指定数据的顺序。 And be sure you have a properly defined index for the field that you are ordering the records based on that. 并确保您为该字段定义了正确的索引,并根据该索引对记录进行排序。

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

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