c# - LINQ,跳过并接受排序

标签 c# linq nhibernate

我正在使用 Take 和 Skip 进行一些测试,我发现:

var objects = (from c in GetObjects() orderby c.Name select c);
var skipTake = objects.Skip(5).Take(10).ToList();
var takeSkip = objects.Take(10).Skip(5).ToList();

GetObjects() 返回由 NHibernate(3.3.3GA,使用 SQL Server 2008 R2 数据库)生成的 IQueryable。
skipTake 和 takeSkip 包含完全相同的 10 个对象序列。
但是如果我写

var objects = (from c in GetObjects() orderby c.Name select c).ToList();
var skipTake = objects.Skip(5).Take(10).ToList();
var takeSkip = objects.Take(10).Skip(5).ToList();

skipTake 包含与上述示例相同的序列,而 takeSkip 包含仅包含 5 个对象的不同序列。

Take 和 Skip 调用在应用于 IQueryable 时会重新排序吗?
我很想对此有所了解。

提前致谢。

最佳答案

看起来这是由于特定版本的 nhibernate 中的一个错误:

http://sourceforge.net/p/nhibernate/news/2013/03/nhiberate-333ga-released/

BEWARE: In versions prior to 3.3.3.CR1, the handling of the LINQ Take() method was flawed - no matter where in the query Take() was placed it was always applied as if it had been placed at the end. 3.3.3 fixes this, so that Take() now correctly follows the .Net semantics. That is, in 3.3.3, the following queries might now give different results:

session.Query<Foo>.OrderBy(...).Take(5).Where(...);
session.Query<Foo>.Where(...).OrderBy(...).Take(5);

Starting with 3.3.3, the first query will generate a subquery to correctly apply the row limit before the where-clause.

关于c# - LINQ,跳过并接受排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30344425/

相关文章:

c# - 以数字作为字段名称的 JsonResult

c# - 在 Visual Studio 中查找所有继承接口(interface)的类

c# - Linq 数据映射 : usage of Storage property on column attribute

C# - Nhibernate 问题

c# - 以编程方式截取网页的屏幕截图

c# - 加入等于一侧的比较键(startswith)

c# - LINQ 是否缓存计算值?

c# - Nhibernate 在 db create 脚本中生成错误

nhibernate - 索引 View 和 Nhibernate - NOEXPAND 提示?

c# - 将目录添加到现有的 .zip 文件