How do I select a Random Row using NHibernate's ICriteria API?

纵然是瞬间 提交于 2019-12-17 16:11:31

问题


Can I select a random row using NHibernate's ICriteria API?


回答1:


Just as cundh2o said, it's DBMS-specific. But you can subclass the Order class and define your own custom ordering. For example, for SQL Server:

public class RandomOrder: Order {
    public RandomOrder() : base("", true) {}
    public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery) {
        return new SqlString("newid()");
    }
}



回答2:


If you are not limited to using ICriteria, I might recommend using HQL instead for selecting a random row, since it may provide more flexibility to use the Random function supplied by your db provider.


IQuery q = NHibernateSession.CreateQuery("your hql statement here")



来源:https://stackoverflow.com/questions/729687/how-do-i-select-a-random-row-using-nhibernates-icriteria-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!