Michael D. Kirkpatrick
Michael D. Kirkpatrick

Reputation: 488

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

I have a function that 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>();
}

Upvotes: 1

Views: 263

Answers (1)

dotjoe
dotjoe

Reputation: 26940

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

Upvotes: 1

Related Questions