按可空整数对 NHibernate 查询结果进行分组
我正在使用 NHibernate 查询一个表,该表具有以下格式的元组:(String, Int?),其中整数可以为空。因此,我想按数字对结果进行分组,然后按字母顺序排序。在获得查询结果后,我可以轻松地执行此操作,但我想让 NHibernate 制定一个执行此操作的查询。这是我想要的结果示例:
alpha, 1
三角洲,4
高尔夫,3
酒店, 2
利马, 5
查理,0
θ, 0
测试版,空
echo, null
我正在寻找的三个分组是:(int > 0)、(int == 0) 和(int = null)。这是我正在使用的查询:
var devices = session.QueryOver<Table>()
.OrderBy(item => item.Number).Desc
.OrderBy(item => item.Name).Asc
.List();
目前,我在查询完成后对它们进行排序,如下所示:
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();
sortedDevices = sortedDevices.Concat(devices.OrderBy(item => item.Name).Where(item => item.Number == null).ToList()).ToList();
Is it possible to get NHibernate to group query like this?
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, null
The 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();
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类似的事情:
something along the lines: