Eager Load Collection and Association

1 view
Skip to first unread message

reach4thelasers

unread,
Nov 6, 2009, 1:59:11 PM11/6/09
to NhLambdaExtensionsUsers
Hey guys,

I'm making a forum with nhibernate and NHLambdas.

A ForumCategory (eg Sport) has a number of forums (e.g. Football,
Tennis), each forum has a number of associations, like Threads, Latest
Thread.

I want to Eager load all the Forums in a ForumCategory, and also eager
load some of the associations in each forums.

in HQL this works: "from ForumCategory category left join fetch
category.Forums forums inner join fetch forums.NewestThread
newestthread"

However when I try to do this in NHLambdas
criteria.SetFetchMode<ForumCategory>(t => t.Forums.???,
FetchMode.Eager);

because Forums is a collection, the ??? in intellisense gives me the
collection methods Add/Remove/Count... rather than the properties/
associations in a forum, so basically how do I eager load all the
Forums, and then eager load an association eg LatestThread from each
forum?

Richard Brown (gmail)

unread,
Nov 6, 2009, 2:20:58 PM11/6/09
to NhLambdaExtensionsUsers
Hi Kev,

I think you can use the syntax:

criteria.SetFetchMode<ForumCategory>(t => t.Forums[0].LatestThread)

I think it's also possible (although I could be wrong) to do something like:

criteria.SetFetchMode<ForumCategory>(t => t.Forums)
.CreateCriteria<ForumCategory>(t => t.Forums)
.SetFetchMode<Forum>(f => f.LatestThread).

Let me know if that's of any help.

Cheers,
Richard

P.S., And example of that syntax in the tests is in
TestExpressionProcessor.TestFindMemberExpressionSubCollectionIndex():
http://code.google.com/p/nhlambdaextensions/source/browse/trunk/Source/Test/TestExpressionProcessor.cs

P.P.S., This was added after an issue was raised ... so you need to be using
version 1.0.8.0.
http://code.google.com/p/nhlambdaextensions/issues/detail?id=11&can=1


--------------------------------------------------
From: "reach4thelasers" <kev.m....@googlemail.com>
Sent: Friday, November 06, 2009 6:59 PM
To: "NhLambdaExtensionsUsers" <nhlambdaext...@googlegroups.com>
Subject: Eager Load Collection and Association

reach4thelasers

unread,
Nov 7, 2009, 12:45:18 PM11/7/09
to NhLambdaExtensionsUsers
Thanks richard, the first one worked great!!

had a bit of trouble with the second one, it worked, but for some
reason didn't populate all the associations.

Thanks for replying!

On Nov 6, 7:20 pm, "Richard Brown \(gmail\)" <fluke...@googlemail.com>
wrote:
> Hi Kev,
>
> I think you can use the syntax:
>
> criteria.SetFetchMode<ForumCategory>(t => t.Forums[0].LatestThread)
>
> I think it's also possible (although I could be wrong) to do something like:
>
> criteria.SetFetchMode<ForumCategory>(t => t.Forums)
> .CreateCriteria<ForumCategory>(t => t.Forums)
>     .SetFetchMode<Forum>(f => f.LatestThread).
>
> Let me know if that's of any help.
>
> Cheers,
>     Richard
>
> P.S., And example of that syntax in the tests is in
> TestExpressionProcessor.TestFindMemberExpressionSubCollectionIndex():http://code.google.com/p/nhlambdaextensions/source/browse/trunk/Sourc...
>
> P.P.S., This was added after an issue was raised ... so you need to be using
> version 1.0.8.0.http://code.google.com/p/nhlambdaextensions/issues/detail?id=11&can=1
>
> --------------------------------------------------
> From: "reach4thelasers" <kev.m.mul...@googlemail.com>
Reply all
Reply to author
Forward
0 new messages