How can I write this LINQ query by using the extension method syntax?
var query = from a in sequenceA
from b in sequenceB
select ...;
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For your future reference, all questions of this form are answered by section 7.16 of the C# specification.
Your specific question is answered by this paragraph:
A query expression with a second
fromclause followed by aselectclauseis translated into
So your query:
Is the same as
(Note that of course this assumes that the “…” is an expression, and not, say, an expression followed by a query continuation.)
hdv’s answer points out that
would also be a logically valid translation, though it is not the translation we actually perform. In the early days of LINQ implementation, this was the translation we chose. However, as you pile on more
fromclauses, it makes the lambdas nest more and more deeply, which then presents the compiler with an enormous problem in type inference. This choice of translation wrecks compiler performance, so we introduced the transparent identifier mechanism to give us a much cheaper way to represent the seamntics of deeply nested scopes.If these subjects interest you:
For more thoughts on why deeply nested lambdas present a hard problem for the compiler to solve, see:
http://blogs.msdn.com/b/ericlippert/archive/2007/03/26/lambda-expressions-vs-anonymous-methods-part-four.aspx
http://blogs.msdn.com/b/ericlippert/archive/2007/03/28/lambda-expressions-vs-anonymous-methods-part-five.aspx
For more information about transparent identifiers, see this post from Wes Dyer, who implemented them in C# 3.0:
http://blogs.msdn.com/b/wesdyer/archive/2006/12/22/transparent-identifiers.aspx
And my series of articles about them:
http://ericlippert.com/2014/07/31/transparent-identifiers-part-one/