Nhibernate: distinct results in second level Collection

只愿长相守 提交于 2019-12-04 14:54:30

You cannot accomplish what you want in a simple manner. I believe that it's the left join fetch of C that causes the duplicates. The generated query is something like this:

select a 
left join a.b b
left join b.c c

and the rows returned by the db will be like this:

1: a1 | b1 | c1
2: a1 | b1 | c2
3: a1 | b2 | c3
4: a1 | b2 | c4
...

Althouh you already have filtered the root entity duplicates (the A's), due to the outer join on C, the DB needs to return repeated entries for the B table for each of C's entry. A simple way to solve this is to filter the items through a utility collection. Depending on your Entity requirements, you may also use a HashSet so it automatically filters them out.

Use an ISet instead of an IList (and map it as a set offcourse, instead as a bag).

A Set does not allow duplicate entities.

I encountered the same problem and was not able to solve the duplicate issue through hql. However, I created IEqualityComparer for all collections and did Disinct() on each Collection to eliminate duplicates on top of the hql result.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!