NHibernate中按关系计数的顺序 [英] Order by relation count in NHibernate

查看:52
本文介绍了NHibernate中按关系计数的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的数据结构:

public class User
{
    public Guid Id {get;set;}
    public string Name {get;set;}
    public IList<Books> Books {get;set}
}

我一直在努力使按书签数量对用户排序(一对多关系)成为可能.

I have been struggeling with making it possible to sort the users by the count of bookmarks (one-to-many relation).

我已经尝试过使用linq,条件和queryover的各种方法,但是都没有运气,因此希望你们中的一个能提供帮助.

I have tried various approaches with both linq, criteria and queryover, but with no luck, and therefore hope one of you could help.

我正在使用分页,因为我有很多用户,所以该解决方案需要在SQL上而不是在Web服务器上的内存中进行查询.

I am using paging, since I have quite a few users, so the solution needs to do the query on the SQL and not in memory on the webserver.

推荐答案

var loadedUser = session.Query<User>()
    .Select(u => new { u, u.Books.Count })
    .ToList()
    .OrderBy(anonym => anonym.Count)
    .Select(anonym => anonym.u);

或使用HQL

select user
from User as user 
    left join user.Books as books
group by user
order by count(books)

这篇关于NHibernate中按关系计数的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆