• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Javen-Studio 咖啡小屋

            http://javenstudio.org - C++ Java 分布式 搜索引擎
            Naven's Research Laboratory - Thinking of Life, Imagination of Future

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              24 隨筆 :: 57 文章 :: 170 評論 :: 4 Trackbacks

            3.3         延遲的查詢賦值(Deferred Query Evaluation

            機(jī)敏的讀者(Observant readers)可能注意到標(biāo)準(zhǔn)的 Where 操作符在 C# 2.0 介紹里是用 yield 構(gòu)造(construct)實(shí)現(xiàn)的。這是所有的標(biāo)準(zhǔn)操作符返回(return)有序列的數(shù)據(jù)(sequences of values)所采用的共同的實(shí)現(xiàn)技術(shù)。yield 關(guān)鍵詞的用途有個(gè)很有趣的好處,就是查詢實(shí)際是直到迭代結(jié)束(iterated over)后才被賦值的,即使是 foreach 語句或者手工使用下面(underlying)的 GetEnumerator MoveNext 方法也是如此。這種延遲的賦值方式允許查詢像基于 IEnumerable<T> 值(IEnumerable<T>-based values)一樣保留著,在每次 yield 方式返回可能不同的結(jié)果(yielding potentially different results)的時(shí)候可以被賦值多次(evaluated multiple times)。

             

            對許多應(yīng)用程序來說,這是期望(desired)得到的準(zhǔn)確的行為(is exactly the behavior)。對應(yīng)用程序想緩存查詢賦值的結(jié)果,ToList ToArray 這兩個(gè)操作符提供用來強(qiáng)制查詢的直接賦值(force the immediate evaluation),以返回一個(gè)含有查詢賦值的結(jié)果的 List<T> 或者Array 數(shù)組。

             

            為了解延遲查詢賦值是怎么工作的,請考察下面一段簡單地查詢一個(gè)數(shù)組的程序:

             

            // declare a variable containing some strings
            string[] names = "Allen""Arthur""Bennett" };

            // declare a variable that represents a query
            IEnumerable<string> ayes = names.Where(s => s[0== 'A');

            // evaluate the query
            foreach (string item in ayes) 
              Console.WriteLine(item);

            // modify the original information source
            names[0= "Bob";

            // evaluate the query again, this time no "Allen"
            foreach (string item in ayes) 
                Console.WriteLine(item);

             

            查詢在每次變量 ayes 迭代結(jié)束(iterated over)時(shí)賦值。為了顯示需要對結(jié)果做一份緩存的copy,我們可以簡單給這個(gè)查詢附加上一個(gè) ToList 一個(gè) ToArray 操作符,如下所示:

             

            // declare a variable containing some strings
            string[] names = "Allen""Arthur""Bennett" };

            // declare a variable that represents the result
            // of an immediate query evaluation
            string[] ayes = names.Where(s => s[0== 'A').ToArray();

            // iterate over the cached query results
            foreach (string item in ayes) 
                Console.WriteLine(item);

            // modifying the original source has no effect on ayes
            names[0= "Bob";

            // iterate over result again, which still contains "Allen"
            foreach (string item in ayes)
                Console.WriteLine(item);

             

            ToList ToArray 都強(qiáng)制查詢的賦值,就像執(zhí)行一個(gè)標(biāo)準(zhǔn)查詢操作符(如 First, ElementAt, Sum, Average, All, 等)返回一個(gè)單獨(dú)的值(singleton values)一樣。

             


            待續(xù), 錯(cuò)誤難免,請批評指正,譯者Naven 2005-10-22

            posted on 2005-10-23 00:08 Javen-Studio 閱讀(735) 評論(1)  編輯 收藏 引用

            評論

            # re: 延遲的查詢賦值(Deferred Query Evaluation) 2005-10-23 18:57 Ninputer
            看來我不僅是機(jī)敏的讀者,而且還試圖用真協(xié)同機(jī)制來實(shí)現(xiàn)和迭代完全同步的查詢。只是還有一點(diǎn)點(diǎn)小問題。。。  回復(fù)  更多評論
              


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            91久久精品国产91性色也| 中文无码久久精品| 久久人人爽人人爽人人AV| 久久夜色撩人精品国产小说| 久久国产精品-国产精品| AAA级久久久精品无码片| a级成人毛片久久| 国产三级精品久久| 久久久久久毛片免费看| 久久久久久久久久久免费精品| 久久99精品九九九久久婷婷| 久久亚洲国产精品123区| 伊人久久大香线蕉综合网站| 一本久久知道综合久久| 乱亲女H秽乱长久久久| 91精品国产综合久久精品| 国产国产成人久久精品| 伊人久久一区二区三区无码| 国产A三级久久精品| 97久久超碰国产精品2021| 国产精品免费久久| 人妻系列无码专区久久五月天| 亚洲?V乱码久久精品蜜桃 | 国产精品久久久久久| 亚洲国产精品人久久| 久久国产成人| 少妇人妻88久久中文字幕| 久久电影网一区| 欧美性猛交xxxx免费看久久久| 色婷婷综合久久久中文字幕| 国产成人精品久久免费动漫| 韩国三级中文字幕hd久久精品| 久久91精品国产91| 伊人久久免费视频| 久久综合给久久狠狠97色| 久久九色综合九色99伊人| 久久精品aⅴ无码中文字字幕重口| 激情五月综合综合久久69| 久久精品人成免费| 亚洲人成精品久久久久| Xx性欧美肥妇精品久久久久久|