开发者

Select n+1 problem

开发者 https://www.devze.com 2022-12-08 05:32 出处:网络
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.

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 开发者_如何学Cof 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号