嵌套"从" LINQ查询前pressed与扩展方法 [英] Nested "from" LINQ query expressed with extension methods

查看:150
本文介绍了嵌套"从" LINQ查询前pressed与扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过使用扩展方法的语法写这个LINQ查询?

  VAR的查询=从在sequenceA
            从B在sequenceB
            选择 ...;


解决方案

有关你未来的参考,这种形式的所有问题都是由C#规范的第7.16回答。

您的具体问题是本段回答:


查询前pression与第二子句后跟一个选择条款

 从E1 X1
在E2 X2
选择v

被翻译成

 (E1)。的SelectMany(X1 = GT; E2,(X1,X2)= GT; 5)


所以,你的查询:

  VAR的查询=从在sequenceA
            从B在sequenceB
            选择 ...;

是一样的。

  VAR的查询=(sequenceA)。的SelectMany(一个= GT; sequenceB,(A,B)=> ...)

(注意,当然这是假定的...是一个前pression,而不是,比方说,一名前pression随后查询延续。)

HDV的回答指出,

  VAR的查询=(sequenceA)。的SelectMany(
    A => (sequenceB)。选择(B = GT; ...));

也将是一个的逻辑的有效转换,虽然它不是我们实际执行转换。在LINQ实施的初期,这是我们选择了翻译。然而,正如你堆在多个条款,它使lambda表达式巢越来越深,然后presents编译器带有的巨大在类型推断问题。翻译沉船编译器性能这一选择,所以我们推出了的透明标识符的机制,给我们一个更便宜的方式重新present深度嵌套的作用域的seamntics。

如果这些科目的兴趣你:

有关为什么深度嵌套lambda表达式present编译器的难题解决了更多的心思,见:

<一个href=\"http://blogs.msdn.com/b/ericlippert/archive/2007/03/26/lambda-ex$p$pssions-vs-anonymous-methods-part-four.aspx\">http://blogs.msdn.com/b/ericlippert/archive/2007/03/26/lambda-ex$p$pssions-vs-anonymous-methods-part-four.aspx

<一个href=\"http://blogs.msdn.com/b/ericlippert/archive/2007/03/28/lambda-ex$p$pssions-vs-anonymous-methods-part-five.aspx\">http://blogs.msdn.com/b/ericlippert/archive/2007/03/28/lambda-ex$p$pssions-vs-anonymous-methods-part-five.aspx

有关透明标识符的详细信息,请参阅这篇文章从韦斯代尔,谁执行他们在C#3.0:

<一个href=\"http://blogs.msdn.com/b/wesdyer/archive/2006/12/22/transparent-identifiers.aspx\">http://blogs.msdn.com/b/wesdyer/archive/2006/12/22/transparent-identifiers.aspx

和我的系列关于他们的文章:

<一个href=\"http://ericlippert.com/2014/07/31/transparent-identifiers-part-one/\">http://ericlippert.com/2014/07/31/transparent-identifiers-part-one/

How can I write this LINQ query by using the extension method syntax?

var query = from a in sequenceA
            from b in sequenceB
            select ...; 

解决方案

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 from clause followed by a select clause

from x1 in e1
from x2 in e2
select v

is translated into

( e1 ) . SelectMany( x1 => e2 , ( x1 , x2 ) => v )


So your query:

var query = from a in sequenceA            
            from b in sequenceB
            select ...;  

Is the same as

var query =  ( sequenceA ) . SelectMany( a => sequenceB , ( a , b ) => ... )

(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

var query =  ( sequenceA ) . SelectMany( 
    a => ( sequenceB ) . Select( b => ... ) );

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 from clauses, 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/

这篇关于嵌套&QUOT;从&QUOT; LINQ查询前pressed与扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆