Dmitrii Lobanov
Dmitrii Lobanov

Reputation: 4977

Let clause as part of join clause

Why it is possible to use this:

from r1 in Enumerable.Range(1, 10)
join q1 in Enumerable.Range(1, 20) on r1 equals q1
select r1;

but not possible to use this:

from r1 in Enumerable.Range(1, 10)
let q = Enumerable.Range(1, 20)
join q1 in q on r1 equals q1
select r1;

The MSDN documentation on let clause lacks some details.

Update: I've just tried to make the expression more clearly readable. In my situation I have some methods chaining that I would like to put to the let clause. Otherwise the join clause became too monstrous. As it is not possible to use the let clause (as Jon mentioned) I've introduced the outer variable, here:

IEnumerable<int> q = Enumerable.Range(1, 10);

from r1 in Enumerable.Range(1, 10)
join q1 in q on r1 equals q1
select r1;

Upvotes: 0

Views: 377

Answers (2)

Yahia
Yahia

Reputation: 70369

AFAIK the join is evaluated BEFORE the let...

Here is blog entry on MSDN explaining this more elaborate - http://blogs.msdn.com/b/wesdyer/archive/2007/01/03/how-linq-to-objects-queries-work.aspx

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500873

Unlike SelectMany, a Join clause can't depend on the "current" value of the other range variables - so this will work:

from r1 in Enumerable.Range(1, 10)
let q = Enumerable.Range(1, 20)
from q1 in q where r1 == q1
select r1;

... but the join won't.

If you could elaborate on what you're trying to do, we may be able to help more.

Upvotes: 3

Related Questions