NHibernate Lambdas 加入有序集合
我有一个实体“人”,一个人有一组朋友(也是人实体),
我想获取特定人的前 10 个朋友,按 LastLogin 排序。
我最好的努力是:
public static IList<Person> GetFriends(Person person, int count)
{
Person personAlias = null;
Person friendAlias = null;
ICriteria criteria = NHibernateSessionManager.Instance.GetSession()
.CreateCriteria(typeof (Person), () => personAlias)
.CreateCriteria(() => personAlias.Friends, () => friendAlias, JoinType.LeftOuterJoin)
.AddOrder(() => friendAlias.LatestLogin, Order.Desc)
.Add<Person>(p => p.ID == person.ID)
.SetMaxResults(count);
return criteria.List<Person>();
}
哪个确实抓住了所有用户朋友,但他们不是按LatestLogin排序的。有什么想法吗?
I have a entity 'Person' a person has a collection of Friends (also Person entities)
I want to get the first 10 Friends of a particular person, ordered by LatestLogin.
My best effort is:
public static IList<Person> GetFriends(Person person, int count)
{
Person personAlias = null;
Person friendAlias = null;
ICriteria criteria = NHibernateSessionManager.Instance.GetSession()
.CreateCriteria(typeof (Person), () => personAlias)
.CreateCriteria(() => personAlias.Friends, () => friendAlias, JoinType.LeftOuterJoin)
.AddOrder(() => friendAlias.LatestLogin, Order.Desc)
.Add<Person>(p => p.ID == person.ID)
.SetMaxResults(count);
return criteria.List<Person>();
}
Which Does grab all the users friends, but they are not ordered by LatestLogin. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道这可能听起来很奇怪,但解决方案是更改以下行:
with:
您必须以相反的方式看待它,才能理解为什么这是必要的,并且在开始时并不明显。
您正在请求具有相同“父”朋友(friendAlias)且 ID == person.ID (.Add(p => p.ID == person.ID)) 的 Person 对象 (personAlias),因此您需要按personAlias.LatestLogin 而不是friendAlias.LatestLogin。
I know it may sound weird but the solution is to change the line:
with:
You have to see it the other way around in order to understand why this is necessary and not obvious at the beginning.
You are requesting Person objects (personAlias) that have the same 'Parent' friend (friendAlias) with ID == person.ID (.Add(p => p.ID == person.ID)) therefore you need to sort by the personAlias.LatestLogin and NOT the friendAlias.LatestLogin.