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")