madcapnmckay
madcapnmckay

Reputation: 16004

Fluent Nhibernate Order-By on Collection

If I have a collection mapped in Fluent NHibernate and I want to apply an order to that collection how do I do that?

Eg:

HasMany(x => x.PastDates)
            .AsBag().Cascade
            .SaveUpdate()
            .KeyColumnNames.Add("EventId")
            .Where(e => e.DateFrom < DateTime.Now.Date)
            .Inverse();

I'm looking for the equivalent of the order-by attribute in HBM files.

Thanks

Upvotes: 2

Views: 8440

Answers (2)

Kevin Berridge
Kevin Berridge

Reputation: 6303

Fluent NHibernate now has an OrderBy method which you can use:

HasMany(x => x.PastDates)
        .AsBag().Cascade
        .SaveUpdate()
        .KeyColumnNames.Add("EventId")
        .Where(e => e.DateFrom < DateTime.Now.Date)
        .Inverse()
        .OrderBy("ColumnName DESC");

Upvotes: 24

Jamie Ide
Jamie Ide

Reputation: 49301

It appears that the "order-by" attribute is not in the FluentNHibernate API. I don't see an issue for it so this may be a conscious omission. You should be able to add it using SetAttribute but this user was unable to get it to work.

HasMany(x => x.PastDates)
        .AsBag().Cascade
        .SaveUpdate()
        .KeyColumnNames.Add("EventId")
        .Where(e => e.DateFrom < DateTime.Now.Date)
        .Inverse()
        .SetAttribute("order-by", "column_name");

Be aware that setting order-by may change the collection type that NHibernate uses; however this does not apply for bags.

Upvotes: 5

Related Questions