I'm using NHibernate to query a table that has tuples in the format: (String, Int?), where the integers can be null. So, I want to group my results by number and then sort alphabetically. I can easily do this after I get the query results, but I would like to get NHibernate to formulate a query that does it. Here's an example of the results I would like:
alpha, 1
delta, 4 golf, 3 hotel, 2 lima, 5 charlie, 0 theta, 0 beta, null echo, nullThe three groupings I'm looking for are: (int > 0), (int == 0), and (int = null). Here's the query I'm using:
var devices = session.QueryOver<Table>()
.OrderBy(item => item.Number).Desc
.OrderBy(item => item.Name).Asc
.List();
Currently, I'm sorting them after the query is done, as such:
List<Table> sortedDevices = devices.OrderBy(item => item.Name).Where(item => item.Number > 0).ToList();
sortedDevices = sortedDevices.Concat(devices.OrderBy(item => item.Name).Where(item => item.Number == 0).ToList()).ToList();
开发者_Go百科 sortedDevices = sortedDevices.Concat(devices.OrderBy(item => item.Name).Where(item => item.Number == null).ToList()).ToList();
Is it possible to get NHibernate to group queries like this?
something along the lines:
session.QueryOver<User>()
.Select(Projections.Alias(Projections
.Conditional(Expression.Gt("Number", 0),
Projections.Constant(1),
Projections.Conditional(Expression.Eq("Number", 0),
Projections.Constant(0),
Projections.Constant(-1))),
"group"))
.OrderBy(Projections.Property("group")).Desc
.ThenBy(table => table.Name).Asc
.List();
精彩评论