views:

200

answers:

1
+2  Q: 

Select n+1 problem

Foo has Title.
Bar references Foo. I have a collection with Bars.
I need a collection with Foo.Title.

If i have 10 bars in collection, i'll call db 10 times.

bars.Select(x=>x.Foo.Title)

At the moment this (using NHibernate Linq and i don't want to drop it) retrieves Bar collection.

var q = from b in Session.Linq<Bar>()
                where ...
                select b;

I read what Ayende says about this.
Another related question.
A bit of documentation.
And another related blog post.
Maybe this can help?
What about this?
Maybe MultiQuery is what i need? :/

But i still can't 'compile' this in proper solution.

How to avoid select n+1?

+2  A: 

This didn't work:

var q = from b in Session.Linq<Bar>().Expand("Foo.Title")
                where ...
                select b;

But this kind a helped:

var q = from b in Session.Linq<Bar>().Expand("Foo")
                where ...
                select b;

..but now thing that's going to use repository does not know that it's loading foos too.
Any ideas how to make it more explicit?

One idea is to change naming to FindBarsWithFoos().

At least it works.

Arnis L.