简体   繁体   中英

Avoid dynamic ordering of results in entity framework

i am fetching records from User table from database using Entity framework with some where condition. when i execute the query in Sql Server 2008 R2 the order in which results appear is different than the result produced by Entity framework. Entity Framework by default orders results based on primary key. but i don't want any ordering. i want records as it is they are appearing in Sql server R2.

maybe you can remove the ordering applied by EF, if any, but your request 'i want the rows in the same order as they appear on sql server' (i read this as 'the same order i see when i run the query from ssms') cannot be satisfied.

in sql (and sql-server also) the rows have no order and when you make a query the rdbms decides the order of the output depending on various factors that are not under your control: current load, cpu, ram, number of rows, indexes, many others.

that means that you cannot expect any ordering , not even the same 'unordered order' you see on ssms.

executing the very same query twice in a row may produce two result sets with the same rows but different order and this is correct and expected.

executing the same query using different tools may produce sets with different ordering as well.

here is an old (but still valid) article with a couple of examples and tests to show that you cannot have any expectation about the order of the result set if you don't specify a order by clause.

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