let's say I have 2 tables table1(a,b) and table2(c,a)
I need to do something like this, but with NHibernate criteria:
select a,b, (select count(*) from table2 t2 where t1.a = t2.a ) x from table1 t1
anybody knows how to do this ?
with 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>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With