0

Is it possible to create a anoynmous count with nhibernate?

The below query throws the exception "No column *". I could of course add a column name, but I'd prefer not to, because if I do, I'll have to lookup column names for 95 tables...

NHibernate.Criterion.DetachedCriteria dcIsUniqueDomainname = NHibernate.Criterion.DetachedCriteria.For<nhDBapi.Tables.clsDomains>()
               .SetProjection(
                   NHibernate.Criterion.Projections.Count("*")
               )
               .Add(NHibernate.Criterion.Property.ForName("DomainID").Eq(strDomainID))
               .Add(NHibernate.Criterion.Property.ForName("DomainName").Eq(strDomainName)
           );
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442

1 Answers1

5

You are Looking for Projections.RowCount().

Diego Mijelshon
  • 52,548
  • 16
  • 116
  • 154
  • Why does the type for NHibernate.Criterion.Projections.RowCount() need to be an int32? It fails when i use long (int64)... I mean there clearly can be more than 2^32 rows, can't there ? And it's kinda ironic when it returns a negative number because of overflow or whatever... – Stefan Steiger Oct 25 '10 at 20:15
  • +1, and RowCount silently is int32... I wonder why it can't convert from int32 to long, the only problem should be vice-versa... – Stefan Steiger Oct 26 '10 at 10:00
  • You *can* convert the result of a RowCount projection to long by using Convert.ToInt64; it just doesn't make much sense. – Diego Mijelshon Oct 26 '10 at 11:09