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