关于Linq+NHibernate的注意事项【排序】

1.Linq中的排序字段的优先级与SQL语句不同,SQL中前者优先而Linq中后者优先,如下语句有等价的排序效果。

--SQL:

select a.is_topmost, a.* from tab_com_attachment a 

order by NVL(a.is_topmost, 0) desc, a.last_modify desc;

//Linq:

var qFiles = from a in db.Attachments

                   where idList.Contains(a.Id)

                   select a;

var tFiles = from a in qFiles.ToList()

                   orderby a.LastModify descending

                   orderby a.IsTopmost.GetValueOrDefault(false) descending  

                   select new FileTransportInfo()

                  {

                      FileId = a.Id,

//......

    };

注意:用可空类型排序时需要先转换成不可空的,而NHibernate3.3不支持直接转换,需要先用ToList()转换成本地数组再排序。

posted @ 2012-10-15 12:46  红萝卜  阅读(772)  评论(0编辑  收藏  举报