Skip to content Skip to sidebar Skip to footer

Ordering By Collection Count In Nhibernate

Is there a way of ordering a list of objects by a count of a property which is a collection? For arguments sake let's say I have a question object with a question name property, a

Solution 1:

I ran into this issue and I used Linq to get around it.

var sort = from n in c
                       orderby q.Answers.Count
                       select n;

I BELIEVE that's the syntax, if someone could verify that would be great I haven't used LINQ too much.

Solution 2:

i can't use linq.

the way i've solved it (and you may decide it's donkey) is to create a computed column on the database that calculates the count using a UDF. i then marked the column in the hibernate mapping as update="false" and insert="false".

i can now order my queries by this new column as normal. It gives me the added bonus of being able to bind this value in places where it need it throughout my app.

it seems to perform ok, hopefully it isn't gonna cost me too much performance.

Post a Comment for "Ordering By Collection Count In Nhibernate"