xll
xll

Reputation: 3109

NHibernate session.Query<ISomeInterface>().Count returns wrong count

I faced the problem with getting correct count of objects by interface:

var count = session.Query<IDirty>().Count();

There are 2 classes that implement interface IDirty. Count returns the number of objects of one class, ignoring the second.

QueryOver just throws an exception that the item is not unique.

var count2 = session.QueryOver<IDirty>().RowCount();

Internally it uses SingleOrDefault method, what explains why it fails...

Getting the list of items is working correctly - list includes objects of both types:

var list= session.Query<IDirty>().ToList();

Is there any workaround to get correct count without enumerating all items?

Upvotes: 2

Views: 814

Answers (2)

Firo
Firo

Reputation: 30813

try

session.QueryOver<IDirty>().ToRowCountQuery().List<int>().Sum()

Upvotes: 3

Diego Mijelshon
Diego Mijelshon

Reputation: 52735

In my opinion, this is a bug, although I can see why it would be non-trivial to fix it.

You can open an issue at https://nhibernate.jira.com. In the meantime, just use separate queries for each type implementing IDirty.

Upvotes: 1

Related Questions