Select n+1 problem
P

1

6

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?

Precipitin answered 5/10, 2009 at 10:17 Comment(0)
P
3

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.

Precipitin answered 5/10, 2009 at 11:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.