I understand that when the C# compiler sees a linq query comprehension, it basically does a straight translation to the corresponding Linq Extension methods and lambdas. i.e.
from x in list
select x.property
gets translated to:
list.Select(x => x.property)
my question is what do let clauses get translated to. for example how would this get translated by the compiler.
from x in list
let v = SomeComplexExpressionDependingOnx
select v
(p.s. i know this could be reduced to just select SomeComplexExpressionDependingOnx but i want to know how this is done in general)
Thanks!
In this particular case, it gets translated to:
But there may be a more complex case, such as:
Will translate to:
In other words, the
letkeyword is a way to minimize and/or optimize your query. That is, without theletkeyword you would have to write:Resulting in possible triple evaluation of the same expression.
Update, following a question in comment.
First, what’s so scary about "block expressions"? They’re just a shorthand for arbitrary delegate. That is, the following expression:
Is equivalent to the following:
Second, what’s so special about "block expressions"? Did you know that mmm… let’s call them "non-block" expressions also expand to the very same code? That is, the simple code
new Func<string,int>( s => s.Length/2 )is absolute equivalent to:Third, what’s so non-linqy about "block expressions"? LINQ uses delegates all over the place, and it doesn’t really matter to LINQ what exact shortcut you use to represent those delegates.
In particular, your expression
from a in list where a.SomeProp > 10 select new { A = a, B = a.GetB() }gets translated into the following:Fourth, to get understanding like this, one can just play with the language and explore.
And fifth, if the previous advice doesn’t work for you for one reason or another, you always have ILSpy. Very useful tool, everybody should have one.