Your problem is here:
To understand why this is a problem, consider the following code:
int n = 1000; var evens = Enumerable.Range(1, 1000).Where(n => n % 2 == 0);
The expression n % 2 == 0 above is ambiguous: what are we talking about n ? If we are talking about external n , then n % 2 == 0 always matters, since n is only 1000 (and therefore evens will contain all numbers from 1 to 1000). On the other hand, if we are talking about internal n , then n % 2 == 0 will be true only for even values โโof n (and evens will be 2, 4, 6, ... 1000).
An important point to implement is that variables declared outside of lambda are accessible from the lambda area.
int n = 0; Action incrementN = () => n++; // accessing an outer variable incrementN(); Console.WriteLine(n); // outputs '1'
That is why there is ambiguity and why it is not allowed.
The solution is simply to choose a different variable name for your lambda; eg:.
var ngt5 = numbers.Where(x => x > 5);
Dan Tao May 27 '11 at 17:31 2011-05-27 17:31
source share