score:1

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.

session.Linq<Blog>().Expand("Posts").ToList().Distinct();

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

score:-3

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?

score:4

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

var blogs = session.Linq<Blog>();
blogs.QueryOptions.RegisterCustomAction(
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 :)

http://nhforge.org/wikis/howtonh/get-unique-results-from-joined-queries.aspx


Related Articles