http://javenstudio.org - C++ Java 分布式 搜索引擎 Naven's Research Laboratory - Thinking of Life, Imagination of Future
Select 操作符需要轉(zhuǎn)換函數(shù)(transform function)用來為信息源序列(source sequence)中的每一個(gè)值(each value)產(chǎn)生一個(gè)值(produce one value)。如果你的轉(zhuǎn)換函數(shù)返回了一個(gè)自己也是一個(gè)序列的值,它等于(is up to)消費(fèi)者(consumer)手工地(manually)穿越(traverse)子序列(sub-sequences)。例于,考察下面的程序,使用已存在的 String.Split 方法分拆這些字符串成一段段的部分(breaks strings into tokens):
運(yùn)行這段程序,將打印出如下的文本:
理想情況下(Ideally),我們希望我們的查詢返回一個(gè)包含一個(gè)個(gè) token 段的結(jié)合的序列(coalesced sequence),而不是暴露(exposed)中間的(intermediate)string[] 給消費(fèi)者(consumer)。為了達(dá)到這個(gè)目的(To achieve this),我們使用 SelectMany 操作符而不用 Select 操作符。SelectMany 操作符使用起來比 Select 更簡單。它們不同的(differs in)地方是轉(zhuǎn)換函數(shù)(transform function)被期望返回一個(gè)序列,然后才再 SelectMany 操作符展開來(expanded by)。這里是一段我們用 SelectMany 重寫的程序:
使用 SelectMany 引起每一個(gè)中間的序列(each intermediate sequence)像普通賦值的一部分(part of normal evaluation)一樣被展開(expanded)。
待續(xù), 錯(cuò)誤難免,請(qǐng)批評(píng)指正,譯者Naven 2005-10-24
Powered by: C++博客 Copyright © Javen-Studio