我正在使用此查询:
return from oi in NHibernateSession.Current.Query<BlaInteraction>()
select new BlaViewModel
{
...
NoPublications = oi.Publications.Count(),
...
};
BlaInteraction包含IList的出版物(即实体)。要确定出版物的数量,实际上并不需要为出版物进行所有连接。我能否阻止nhibernate在生成的sql中使用连接(例如使用投影???)?
感谢。
基督教
PS:
这就是NH产生的(略微适应):
select cast(count(*) as INT) from RelationshipStatementPublications publicatio21_, Publication publicatio22_ inner join Statements publicatio22_1_ on publicatio22_.StatementId=publicatio22_1_.DBId where publicatio21_.StatementId = 22762181 and publicatio21_.PublicationId=publicatio22_.StatementId
这就足够了:
select cast(count(*) as INT) from RelationshipStatementPublications publicatio21_ where publicatio21_.StatementId = 22762181
答案 0 :(得分:3)
为什么不能只创建另一个查询?
Session.QueryOver<Publication>().Where(x => x.BlaInteractionId == idSentAsParameter).Select(Projections.RowCount()).SingleOrDefault<int>();
我认为这样可行
return from oi in NHibernateSession.Current.Query<BlaInteraction>()
select new BlaViewModel
{
...
NoPublications = Session.QueryOver<Publication>().Where(x => x.BlaInteractionId == oi.Id).Select(Projections.RowCount()).SingleOrDefault<int>();
...
};
另一个编辑,你试过lazy="extra"吗?
答案 1 :(得分:2)
好的我到目前为止找到的最佳解决方案是使用FNH公式:
mapping.Map(x => x.NOPublications).Formula("(select count(distinct RelationshipStatementPublications.PublicationId) from RelationshipStatementPublications where RelationshipStatementPublications.StatementId = DBId)");
public virtual int NOPublications {get; private set;}
当我从域映射到视图模型时,我使用:
NoPublications = oi.NOPublications,
基督教