• <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

            4           其他標(biāo)準(zhǔn)查詢操作符(More Standard Query Operators

            在最上面描述的基本的查詢技巧(query facilities)中,許多操作符(a number of operators)提供有用的方式(useful ways)來操作序列(manipulating sequences)和組裝查詢(composing queries),通過使用便利(convenient)的標(biāo)準(zhǔn)查詢操作符(standard query operators)的框架(framework)讓用戶在結(jié)果(over the result)之上有很高的控制程度(high degree of control)。

             

            4.1         排序和分組操作(Sorting and Grouping

            通常(In general),查詢表達(dá)式(query expression)的賦值(evaluation)導(dǎo)致(產(chǎn)生)(results in)的一個序列的值(sequence of values),并且以某種順序(in some order)產(chǎn)生(produced),這是基本的信息源(the underlying information sources)的固有性質(zhì)(intrinsic)。為了讓開發(fā)者清楚地控制(explicit control)數(shù)據(jù)序列產(chǎn)生的順序,所以定義了標(biāo)準(zhǔn)查詢操作符(standard query operators)來控制順序(controlling the order)。其中最基本的操作符就是 OrderBy 。

             

            OrderBy OrderByDescending 操作符可以用來應(yīng)用于任何信息源(any information source),允許用戶提供一個制造出(produces)用來給結(jié)果排序(used to sort the results)的值的一個關(guān)鍵詞分解函數(shù)(key extraction function)。OrderBy OrderByDescending 操作符也可以接收(accept)可選的比較函數(shù)(optional comparison function),這個函數(shù)可以用來給關(guān)鍵詞(keys)的排序強加上(impose)一個次序的排序(partial order)。讓我們看看下面這段基礎(chǔ)的例子:

             

            string[] names = "Burke""Connor""Frank""Everett"
                               
            "Albert""George""Harris""David" }
            ;

            // unity sort
            var s1 = names.OrderBy(s => s); 
            var s2 
            = names.OrderByDescending(s => s);

            // sort by length
            var s3 = names.OrderBy(s => s.Length); 
            var s4 
            = names.OrderByDescending(s => s.Length);

             

            前面兩個查詢表達(dá)式產(chǎn)生新的序列是基于字符串比較(based on string comparison)的對數(shù)據(jù)源成員的排序方式(based on sorting the members of the source)產(chǎn)生的。下面兩個查詢產(chǎn)生的序列是基于每個字符串長度(based on the length of each string)的對數(shù)據(jù)源成員的排序(sorting the members of the source)方式產(chǎn)生的。

             

            為了允許多次排序的標(biāo)準(zhǔn)(multiple sort criteria),OrderBy OrderByDescending 操作符都返回 SortedSequence<T> 接口 而不是通常的 IEnumerable<T>.接口。兩個只定義在 SortedSequence<T> 接口之上的操作符,被稱作 ThenBy ThenByDescending,它們用來應(yīng)用一個附加的(并且次序的)的排序標(biāo)準(zhǔn)(additional (subordinate) sort criterion)。ThenBy/ThenByDescending 操作符它們自己返回 SortedSequence<T> 接口,而且允許應(yīng)用許多(any number of)次 ThenBy/ThenByDescending 操作符:

             

            string[] names = "Burke""Connor""Frank""Everett"
                               
            "Albert""George""Harris""David" }
            ;

            var s1 
            = names.OrderBy(s => s.Length).ThenBy(s => s);

             

            在這個例子中,變量 s1 所引用(referenced)的查詢的賦值(Evaluating the query)將產(chǎn)生出(yield)下面的值的序列(sequence of values):

             

            "Burke""David""Frank"
            "Albert""Connor""George""Harris"
            "Everett"

             

            除了(In addition toOrderBy 種類的操作符(OrderBy family of operators),標(biāo)準(zhǔn)查詢操作符(standard query operators)還包括一個 Reverse 操作符。Reverse 操作符簡單地在一個序列上進(jìn)行枚舉(enumerates)操作,產(chǎn)生(yields)一個以相反順序排序(in reverse order)的包含相同成員的一組值。與 OrderBy 不同,Reverse 在決定排序(determining the order)的時候不考慮那些值自己的真實內(nèi)容(actual values themselves),而是單獨地依靠(relies solely)基本數(shù)據(jù)源(the underlying source)所產(chǎn)成的值的順序。

             

            OrderBy 操作符影響(imposes)一組值的序列(a sequence of values)的排列順序(a sort order)。標(biāo)準(zhǔn)查詢操作符還包括 GroupBy 操作符,它影響一組值的序列的分割(partitioning,基于一個關(guān)鍵詞分解函數(shù)(a key extraction function)。GroupBy 操作符返回一個分組的值(Grouping values)的序列,一組會遇到的每一個單獨的關(guān)鍵詞的值的序列(one for each distinct key value that was encountered)。每一分組(each grouping)既包含了關(guān)鍵詞(key)又(as well as)包含了映射到(mapped to)這個 key 的那一組值(the group of values)。分組(Grouping)的公開標(biāo)識(public contract)看起來像如下的代碼:

             

            public sealed class Grouping<K, T> {
              
            public Grouping(K key, IEnumerable<T> group);
              
            public Grouping();
              
            public K Key getset; }
              
            public IEnumerable<T> Group setget; }
            }

             

            最簡單的使用 GroupBy 程序看起來如下所示:

             

            string[] names = "Albert""Burke""Connor""David",
                               
            "Everett""Frank""George""Harris"}
            ;

            // group by length
            var grouping = names.GroupBy(s => s.Length);

            foreach (Grouping<intstring> group in grouping) {
                Console.WriteLine(
            "Strings of length {0}", group.Key);

                
            foreach (string value in group.Group)
                    Console.WriteLine(
            "  {0}", value);
            }
                

             

            當(dāng)運行后,此段程序?qū)⒋蛴〕鋈缦碌慕Y(jié)果:

             

            Strings of length 6
              Albert
              Connor
              George
              Harris
            Strings of length 
            5
              Burke
              David
              Frank
            Strings of length 
            7
              Everett

             

            Select GroupBy 關(guān)鍵詞允許你提供一個映射函數(shù)(projection function)來移動分組的成員(populate members of the groups):

             

            string[] names = "Albert""Burke""Connor""David",
                               
            "Everett""Frank""George""Harris"}
            ;

            // group by length
            var grouping = names.GroupBy(s => s.Length, 
                                         s 
            => s[0]);
            foreach (Grouping<intchar> group in grouping) {
                Console.WriteLine(
            "Strings of length {0}", group.Key);

                
            foreach (char value in group.Group)
                    Console.WriteLine(
            "  {0}", value);
            }
              

             

            這個變化(variation)將打印出如下結(jié)果:

             

            Strings of length 6
              A
              C
              G
              H
            Strings of length 
            5
              B
              D
              F
            Strings of length 
            7
              E

             

            從這個例子中需要注意的是,投影類型(projected type)不需要跟數(shù)據(jù)源一模一樣。既然這樣(In this case),我們從一個字符串的序列(a sequence of strings)創(chuàng)建了一個從整型(integers)到字符(characters)的分組。

             

             

             

             

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

            posted on 2005-10-25 00:32 Javen-Studio 閱讀(1100) 評論(0)  編輯 收藏 引用
            国产精品美女久久久久网| 国产一区二区精品久久 | 久久99精品久久久久久齐齐 | 伊人久久亚洲综合影院| 超级碰碰碰碰97久久久久| 欧美噜噜久久久XXX| 99久久精品国产综合一区| 久久综合久久综合亚洲| 久久久无码精品亚洲日韩蜜臀浪潮| 99久久国产综合精品麻豆| 久久久久亚洲AV无码专区网站| 久久综合亚洲色HEZYO国产| 久久精品国产亚洲AV无码麻豆| 国产三级精品久久| 伊人久久综合无码成人网| 狠狠精品干练久久久无码中文字幕 | 一本久久a久久精品vr综合| 日本免费久久久久久久网站| 婷婷国产天堂久久综合五月| 91精品国产综合久久香蕉| 中文字幕无码免费久久| 精品国产91久久久久久久a| 久久无码人妻一区二区三区| 亚洲色欲久久久久综合网| 国产精品99久久久久久猫咪| 久久精品国产第一区二区三区| 性做久久久久久久久久久| 91精品久久久久久无码| 国内精品久久久久| 久久精品国产第一区二区三区| 婷婷久久五月天| 亚洲AV伊人久久青青草原| 久久久久噜噜噜亚洲熟女综合| 久久精品成人国产午夜| 国产精品久久影院| 97久久久精品综合88久久| 亚洲欧美日韩中文久久| 久久亚洲国产精品成人AV秋霞| 久久笫一福利免费导航| 国产精品亚洲综合久久| 久久久噜噜噜久久中文字幕色伊伊|