OrderByDescending(OrderBy)和AddOrder之间的区别

时间:2012-09-04 08:48:08

标签: linq nhibernate hibernate-criteria

此代码

criteria
.AddOrder(new Order("AnnoInizialePfk", false))
.List<MyClass>()        
.ToDictionary(x => (short)x.AnnoInizialePfk, x => Convert.ToString(x.AnnoInizialePfk));

生成这个sql

SELECT this_.ANNO_INIZIALE_PFK as y0_ FROM dbo.Table this_ ORDER BY y0_ desc

代替此代码

criteria
.List<MyClass>()
.OrderByDescending(x => x.AnnoInizialePfk)
.ToDictionary(x => (short)x.AnnoInizialePfk, x => Convert.ToString(x.AnnoInizialePfk));

生成这个sql

SELECT this_.ANNO_PFK as y0_ FROM dbo.Table this_

结果集都按降序排序,但在第二个代码中怎么可能?

哪种代码更好用?

由于

萨拉

1 个答案:

答案 0 :(得分:1)

第二个语句中的List()调用使其直接枚举Select。即'OrderBy'在内存中完成,而不是在DB中完成。您应该交换OrderBy和List调用(尽管您不需要List,因为它是多余的,因为ToDictionary也会这样做。)