3
我有兩個簡單的類集結左連接NHibernate的使用QueryOver /的ICriteria
public class Blog
{
public Blog(){
Comments=new List<Comment>();
}
public virtual Guid Id { get; set; }
public virtual string Title { get; set; }
public virtual string Text { get; set; }
public virtual DateTime CreatedDate { get; set; }
public virtual IList<Comment> Comments { get; set; }
}
和
public class Comment
{
public virtual Guid Id { get; set; }
public virtual string Author { get; set; }
public virtual string Text { get; set; }
}
使用自動地圖映射和所有是偉大的世界。我可以添加和保存實體沒有問題。
我希望做的是使用QueryOver獲得的每博客評論的數量,而且還包括這些博客那裏有沒有意見,所以在SQL:
SELECT b.Title,COUNT(c.ID) AS Comments
FROM Blogs b LEFT JOIN Comments c ON b.ID=c.BlogID
,並得到
Title Comments Blog 1 0 Blog 2 0 Blog 3 0 Blog 4 4 Blog 5 0
我得到的最接近是
var results=session.QueryOver<Blog>()
.Left.JoinQueryOver<Comment>(b=>b.Comments)
.TransformUsing(new DistinctRootEntityResultTransformer())
.List<Blog>()
.Select(b => new {
Id = b.Id,
Title = b.Title,
Comments=b.Comments.Count
});
它得到正確的答案,但SQL運行作爲
SELECT b.Id,b.Title,c.ID,c.Author,etc... AS Comments
FROM Blogs b LEFT JOIN Comments c ON b.ID=c.BlogID
然後在客戶端進行計數,這似乎並不是最有效的方法。
這可以通過QueryOver或ICriteria完成嗎?如果可能,我寧願不使用hql。如果你想看到所有的配置等
乾杯
B.