假设我有2张桌子 table1(a,b)和table2(c,a)
我需要做这样的事情,但是使用NHibernate标准:
select a,b, (select count(*) from table2 t2 where t1.a = t2.a ) x from table1 t1
有谁知道怎么做?
答案 0 :(得分:7)
使用Projections.SubQuery
var l = session.CreateCriteria<Table1>("t1")
.SetProjection(Projections.ProjectionList()
.Add(Projections.Property("a"), "a")
.Add(Projections.Property("b"), "b")
.Add(Projections.SubQuery(
DetachedCriteria.For<Table2>("t2")
.SetProjection(Projections.RowCount())
.Add(Restrictions.EqProperty("t1.a", "t2.a"))), "x"))
.SetResultTransformer(Transformers.AliasToBean<Table1WithTable2Count>())
.List<Table1WithTable2Count>();
答案 1 :(得分:1)