开发者

NHibernate: Child collection count and eager fetch of child collection properties

I've just started out using NHibernate and I have 2 issues I have encountered. I'm needing to get a row count of a child collection in my aggregate root, and was wanting to know the best way to go about 开发者_运维百科doing it. I have the following for my query so far:

 var query = QueryOver.Of<Idea>()
             .Where(i => i.Status != IdeaStatus.Completed)
             .Fetch(x => x.Category).Eager
             .Fetch(x => x.Comments).Eager
             .Fetch(x => x.Votes).Eager
             .Fetch(x => x.CreatedBy).Eager
             .OrderBy(i => i.Created).Desc.Cacheable();

        if (Category != 0)
            query = query.Where(i => i.Category.Id == Category);

I'm needing a count of the Comments collection rather than eagerly fetching all the items back. I have a property on my Idea entity called CommentCount that I would like to populate from this. The second issue is that I'm eagerly fetching all Votes for an Idea, however when I iterate through the votes for each idea it seems to be querying the DB again - I guess this is because I have a CreatedBy property which refers to a User entity - so I was wondering how I would specify that I want CreatedBy property in the child collection to be eagerly fetched aswell.

Many thanks in advance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜