Choose problem n + 1

Foo has a name.
Bar links foo. I have a collection with bars.
I need a collection with Foo.Title.

If I have 10 bars in the collection, I will call db 10 times.

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

Right now (using NHibernate Linq and I don't want to drop it) retrieves the Bar collection.

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

I read that Ayende talks about this .
Another related question .
A bit of documentation .
And one more related message.
Maybe this can help?
What about this one ?
Perhaps MultiQuery is what I need?: /

But I still canโ€™t โ€œcompileโ€ this in the correct solution.

How to avoid choosing n + 1?

+6
select-n-plus-1 nhibernate domain-driven-design fluent-nhibernate linq-to-nhibernate
source share
1 answer

This did not work:

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

But this view helped:

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

.. but now the thing that is going to use the repository does not know that it loads foos too.
Any ideas how to make it more explicit?

One idea is to change the name to FindBarsWithFoos ().

At least it works.

+3
source share

All Articles