【发布时间】:2011-08-16 08:08:55
【问题描述】:
我很难弄清楚如何执行以下操作。
给定以下类:
public class Post
{
...
public IList<Comment> Comments
...
}
public class Comment
{
public DateTime CommentDate
... Some other properties but no reference to Post...
}
如何编写查询以仅获取按日期降序排列的给定帖子的前 10 个 cmets?
由于没有从Comment 到Post 的引用,我无法查询Comment,我需要查询Post,但我的所有查询似乎都返回Post,并且我的尝试投影失败。
我无法从Comment 添加引用Post 的属性(顺便说一句,这实际上不是我的域模型),所以我被卡住了。
我希望我没有遗漏一些明显的东西。
编辑:
如果有从评论到帖子的引用,这将给我我想要的东西
var query = (from comment in Session.Query<Comment>() orderby comment.CommentDate
where comment.Post == some Post select comment).Take(10);
但没有,所以我在 Post 上寻找返回 10 条评论列表的等效查询。
如果可以通过 Linq 进行查询,那是我更喜欢的,但使用 QueryOver 会很高兴。
我可能只是最终改写我的领域模型,以便有那个参考。
【问题讨论】:
-
发布您遇到的问题的代码,以便我们更好地了解如何回答问题(例如,我们是否为您提供 HQL、Criteria、QueryOver 等)。
标签: nhibernate nhibernate-criteria