开发者

When using CreateCriteria how do you sort the results in random order?

开发者 https://www.devze.com 2023-01-29 06:49 出处:网络
I have a function that开发者_StackOverflow returns a max number of items that are active. I would like to be able to make the returned results be sorted in random order.

I have a function that开发者_StackOverflow returns a max number of items that are active.

I would like to be able to make the returned results be sorted in random order.

public IList<Widgets> GetWidgetsToDisplay(int maxToGet)
{
    var query = CommonSessionManager.GetSession().CreateCriteria<Widgets>()
        .Add(Expression.Eq("IsDeleted", false))
        .Add(Expression.Eq("IsActive", true));

    return query.SetMaxResults(maxToGet).List<Widgets>();
}


This will be database dependent but have a look here for a sql server order by newid() solution.

0

精彩评论

暂无评论...
验证码 换一张
取 消