views:

31

answers:

1

If I have something like this:

x = from f in first.Include("second")
    where f.id == 1
    select f

y = from s in second
    where s.first.id == 1
    select s

Will two queries be sent to my database? I realize that I could just set y = f.second to ensure that only one call is made, but I frequently want to factor my code such that I can do the second call independent of whether I've done the first - having to make an overload where you can pass in f.second is annoying.

+1  A: 

Assuming you Enumerate the queries two calls would be made.

but I frequently want to factor my code such that I can do the second call independent of whether I've done the first

Do you have a reason why you want to keep the queries separate? The two queries you wrote are the same. You have already retrieved all the data you need in query x. You dont need to do the second query because you can just do

 x.Seconds;
 //or if there is more than one result in x
 x.SelectMany(y=>y.Seconds);
Nix
@Nix -- You are correct sir. I mistyped what I had. However, your answer is better so I am going to remove mine.
Josh
well, I might have two methods: GetFirstStuff() and GetSecondStuff(). I don't want to have to write two paths in GetSecondStuff depending on whether GetFirstStuff has been called or not. It's not the end of the world, just annoying. Oh well, thanks for the answer.
Xodarap