:)
I have the following classes:
- Dislike
- DislikedComment (Comment)
- MemberId (int)
- Comment
- Dislikes (List<Dislike>)
- Text (String)
- ArticleId (int)开发者_JS百科
(Extra properties omitted for brevity)
These classes are mapped using Fluent NHibernate in the expected way (with inverse on the list on comment). Now what I would like is for NHibernate to load the size of the Dislikes collection on Comment when loading the comment, but without loading the entire collection. I would very much like to avoid formulas (yuk @ native sql), and I would prefer for NHibernate to only fire one query. Is this in any way possible?
I know about the Extra Lazy feature, but as far as I know, this fires an extra query, which is not optimal with a list of hundreds of comments.
It is a common misconseption that you need 1 query for best perfomance.
Each join requires the database to perform additional work, and the complexity and cost of the query grows rapidly with each additional join. While relational database are optimized for handling joins, it is often more efficient to perform several separate queries instead of a single query with several joins in it.
in your case i would do 2 queries.
var dislikeCount = _session.Query<Comment>().Count(x=>x.Dislike);
var comments = _session.Query<Comment>().ToList();
ps: dont forget to wrap it inside a transaction.
精彩评论