如何使用nhibernate选择5个随机记录。
我的sql看起来像这样:
SELECT TOP 5 u.UserId, u.UserName, p.ImageFileName FROM users as u, profiles as p WHERE u.UserId = p.UserId ORDER BY NEWID()
我尝试过这样做,但它不起作用
IList<User> users = session
.CreateCriteria(typeof(User))
.CreateCriteria("Profile")
.Add<Profile>(p => p.ImageFileName != string.Empty)
.AddOrder(Order.Asc("NEWID()"))
.SetMaxResults(5)
.List<User>();
答案 0 :(得分:1)
您可以使用SQLQuery:
var query =
"SELECT TOP 5 u.UserId, u.UserName, p.ImageFileName " +
"FROM users as u, profiles as p " +
"WHERE u.UserId = p.UserId ORDER BY NEWID()";
ISQLQuery qry = session.CreateSQLQuery(query).AddEntity(typeof(User));
List<User> rndUsers = qry.List<User>();