gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:38:17 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com