Accepted answer

Distinct is what you need...

Edit: When it doesn't work: do the distinct after the tolist. I don't know why NHibernate loads the same number of objects as the number of database records returned and doesn't do the distinct automatically. This issue/feature is not Linq specific, but will also happen when you use criteria or hql.


Sometimes it can be more efficient to execute 2 queries (seperate, or using multiquery/future) than executing one query with a left outer join.


We have just the same problem. It seems to me that linq is always in eager loading mode. So you don't need to do exapnd. However it is very bad. Have you tried to contact HN guys in their google group?


After searching other forums (perhaps I should of done this properly first!) I'm using this solution:

var blogs = session.Linq<Blog>();
criteria => criteria.SetResultTransformer(new DistinctRootEntityResultTransformer()));
var results = blogs.Expand("Posts");

I didn't want to use Distinct as I wanted to return IQueryable

Seems to work. I just need to know the theory :)

Related Articles