c# - Linq to NHibernate 排序未按预期工作

标签 c# linq nhibernate linq-to-nhibernate

当执行下面的代码时,当我在内存和 SQL 排序中执行时,我得到了不同的结果。任何人都可以解释为什么会这样。我希望查询以完全相同的顺序返回记录。

       IQueryable<WaitListListItem> query = GetQuery();

        query = query.OrderBy(f => f.CourseNo);

        int pageSize = 14;
        int startRecord = 6;

        IList<WaitListListItem> list1 = query.Skip(startRecord).Take(pageSize).ToList(); // database query paging
        IList<WaitListListItem> list2 = query.ToList().Skip(startRecord).Take(pageSize).ToList(); // In Memory paging

        Print(pageSize, list1, list2);

        int pageSize2 = 14;
        int startRecord2 = 0;

        IList<WaitListListItem> list3 = query.Skip(startRecord2).Take(pageSize2).ToList(); // database query paging
        IList<WaitListListItem> list4 = query.ToList(); // No Paging

        Print(pageSize2, list3, list4);

        Console.ReadLine();

最佳答案

由于多种原因,内存排序可能会有所不同。假设 CourseNo 是字符串而不是数字:

  1. 数据库中的区分大小写设置
  2. 进行字符串比较时的文化设置
  3. 相同记录的排序 - 出于所有意图和目的,此类记录的结果顺序应被视为随机

由于您在代码示例中没有指定以上任何一项,因此很难确切地说出您的情况,但是如果您希望顺序完全相同,那么您应该确保上述每个问题在您的数据库和应用程序中以相同的方式处理。

关于c# - Linq to NHibernate 排序未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26725803/

相关文章:

c# - SQLite 错误 : The 'DbProviderFactories' section can only appear once per config file (IBM Client Access)

c# - EWS 托管 API : Can I load properties for multiple items with one EWS call, 仅给出项目 ID?

c# - Fluent api 级联删除与一个表的关系

c# - 尝试使用 Expression<Func<..>> 从方法调用 Any() 时出现类型错误

.net - 第一个 linq 的第一个问题

C# GC 不释放内存

c# - XML Linq 新手问题

c# - 带聚合的分页投影 nhibernate 查询的总结果

c# - Fluent Nhibernate 按条件映射到不同的列

c# - 使用 Nhibernate 检查已用 key 的最佳方法?