Question

I have a Person class. A person class contains a collection of Friends (also Person objects). A person class also has a LatestLogin property which is the LatestLogin time.

For a given person, I want to return their first 10 friends ordered by descending LatestLogin.

HQL I can do no problem: select friends from Person person inner join person.Friends friends where person = :person order by friends.LatestLogin desc

How do I write this in a Criteria Query? I don't want the containing person object, just a List of the person's friends ordered by LatestLogin.

Was it helpful?

Solution

Here it is:

var cachedPosts = Session.CreateCriteria<Person>("main")
                .CreateCriteria("Friends", "f")
                .Add(Restrictions.Eq("f.Id", person.ID))
                .AddOrder(Order.Desc("main.LatestLogin"))
                .List<Person>();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top