简体   繁体   English

NHibernate session.Query <ISomeInterface> ().Count返回错误的计数

[英]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. 有2个类实现接口IDirty。 Count returns the number of objects of one class, ignoring the second. Count返回一个类的对象数,而忽略第二个。

QueryOver just throws an exception that the item is not unique. QueryOver只是抛出一个异常,即该项目不是唯一的。

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

Internally it uses SingleOrDefault method, what explains why it fails... 它内部使用SingleOrDefault方法,这解释了为什么失败的原因...

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? 是否有任何解决方法可在不枚举所有项目的情况下获得正确的计数?

尝试

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

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 . 您可以在https://nhibernate.jira.com上打开问题。 In the meantime, just use separate queries for each type implementing IDirty. 同时,只需对实现IDirty.每种类型使用单独的查询IDirty.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM