Nhibernate QueryOver. OrderBy using strings property names.
Asked Answered
S

2

9

I'm refactoring old-style query CreateCriteria() to QueryOver(). My Wcf service gets string PropertyName to order queries results. For IQueryable I use Dynamic LINQ to do such ordering, for CreateCriteria() - AddOrder().

IList<object[]> result =
            GetSession()
                .QueryOver(() => activity)
                .JoinAlias(() => activity.ActivityLicense, () => license)
                .Select(Projections.ProjectionList()
                            .Add(Projections.Count<Activity>(e => e.Id), "ActivityCount")
                            .Add(Projections.Group(() => license.SerialNumber), "SerialNumber")
                            .Add(Projections.Count<Activity>(e => e.MacAdress), "MacAddressCount")
                            .Add(Projections.Count<Activity>(e => e.IpAdress), "IpAddressCount")
                )
                .OrderByAlias("ActivityCount") // Compilation Error - I need such extension method
                .List<object[]>();

Any suggestions how to do ordering in case with string property names?

PS: I could not use LINQ to Nhibernate: LINQ to NHibernate - .GroupBy().Skip().Take() cause an exception

Thanks!

Spica answered 13/8, 2012 at 13:34 Comment(0)
A
16

You can always get the UnderlyingCriteria...

var q = GetSession()
                .QueryOver(() => activity)
                .JoinAlias(() => activity.ActivityLicense, () => license)
                .Select(Projections.ProjectionList()
                            .Add(Projections.Count<Activity>(e => e.Id), "ActivityCount")
                            .Add(Projections.Group(() => license.SerialNumber), "SerialNumber")
                            .Add(Projections.Count<Activity>(e => e.MacAdress), "MacAddressCount")
                            .Add(Projections.Count<Activity>(e => e.IpAdress), "IpAddressCount")
                );


q.UnderlyingCriteria.AddOrder(new Order("ActivityCount", true));

var results = q.List();

or as an extension method for IQueryOver

public static IQueryOver<T,T> OrderByAlias(this IQueryOver<T,T> q, string aliasName, bool ascending)
{
    q.UnderlyingCriteria.AddOrder(new Order(aliasName, ascending));
    return q;
}
Aestivation answered 13/8, 2012 at 14:47 Comment(2)
Thanks. Is it possible to turn ICriteria back to IQueryOver?Spica
I don't think so, what you could do is refactor that into an IQueryOver extension method. I'll post example...Aestivation
B
0

You can set the OrderBy directly from the QueryOver API by passing in Projections.Property(propName), for example:

var query = GetSession()
                .QueryOver<Activity>()
                .OrderBy(Projections.Property("ActivityCount").Desc;

There is no way to set the direction by a string, so you'll have to do a simple if/else or create an extension method to simplify the API.

Boggs answered 23/1, 2013 at 6:1 Comment(1)
.Desc property does not existHorn

© 2022 - 2024 — McMap. All rights reserved.