将简单的SelectMany转换为查询语法 [英] Convert simple SelectMany into query syntax

查看:89
本文介绍了将简单的SelectMany转换为查询语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是SelectMany()的简单形式.我们如何才能将其转换为查询语法?

The following is the simple form of SelectMany(). How if at all can we convert this into query syntax?

var array = new string[] { "Shaun", "Luttin" };
array
    .SelectMany(
        s => s
    );

我能做的最好的事情产生相同的输出,但是引入了一个新的变量c ...

The best that I can do produces the same output but introduces a new variable c...

var query = 
    from s in array.AsQueryable()
    from c in s
    select c;

...并产生以下流利的语法.

...and results in the following fluent syntax.

array
   .SelectMany (
      s => s, 
      (s, c) => c
   );

回复:可能重复

我已经阅读了 Is的答案 我担心答案的翻译不会编译回原始的流利语法.

推荐答案

编译器执行翻译,以将查询语法转换为方法语法.详细信息在C#5规范的7.6.12节中指定.快速搜索仅显示了几次翻译,这些翻译可能导致调用SelectMany,所有这些均在7.6.12.4节中:

The compiler performs a translation to turn query syntax into method syntax. The details are specified in section 7.6.12 of the C# 5 spec. A quick search turns up only a couple translations that can result in a call to SelectMany, all in section 7.6.12.4:

带有第二个from子句和一个select子句的查询表达式:
从e1中的x1
来自e2中的x2
选择v
被翻译成 ( e1 ) . SelectMany( x1 => e2 , ( x1 , x2 ) => v )

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 )

一个查询表达式,带有第二个from子句,后跟一个select子句之外的其他内容:
从e1中的x1
来自e2中的x2

被翻译成
from * in ( e1 ) . SelectMany( x1 => e2 , ( x1 , x2 ) => new { x1 , x2 } ) …

A query expression with a second from clause followed by something other than a select clause:
from x1 in e1
from x2 in e2

is translated into
from * in ( e1 ) . SelectMany( x1 => e2 , ( x1 , x2 ) => new { x1 , x2 } ) …

因此似乎没有翻译会导致调用SelectMany的另一个重载.

So there appears to be no translation that results in the other overload of SelectMany being called.

这篇关于将简单的SelectMany转换为查询语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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