C# 標準查詢運算式

來源:互聯網
上載者:User

標籤:des   style   blog   io   ar   color   使用   sp   for   

一、標準查詢運算子

1、C#提供了標準查詢運算子,例如我想選擇專利一系列(pantents)中以年份19開頭的專利,可以用如下語句:

            IEnumerable<Patent>  pantentWhere = pantents.Where(pantent =>                                                                 pantent.YearOfPublicaton.StartsWith("19"));

當然,此處的語句只是定義了查詢,此時pantentWhere並沒有內容,後面Lambda運算式指定的查詢並沒有執行,只有當遍曆pantentWhere集合的時候才開始執行這個查詢規則,這是C#中標準查詢的“延遲執行”

2、投射

專利類包含了 名字  年份  應用號  發明者 等,如果我想將專利類的集合中 每個專利的類型都變為只包含 名字與年份的類型,那麼可以使用select做到,代碼如下:

1 var pantentSelect = pantents.Select(2                 pantent => 3                 { 4                     return new 5                     { 6                         Title = pantent.Title, 7                         Year = pantent.YearOfPublicaton 8                     }; 9                 });

可以看到,Lambda運算式返回了一個包含 名字與年份的類型。而當遍曆pantentSelect時,其投射語句執行,它則是有[(姓名,值),(年份,值)]構成的集合。

3、排序

利用標準查詢運算子OrderByDescending 與 ThenByDescending 可以完成多條件的排序,代碼如下:

1 IEnumerable<Patent> pantentOrder = pantents.OrderByDescending(pantent => 2                                                           pantent.YearOfPublicaton).ThenByDescending(3                                                           pantent => pantent.Title);

可以看到,只用了一個OrderBy,它會擷取並且只會擷取一個成為KeySelector的參數來排序,例如本例中的YearOfPublicaton。如果要繼續按照第二個關鍵字排序,只能用ThenBy,在OrderBy的基礎上執行。而連著使用多個OrderBy只會撤銷上一個OrderBy,所以要用ThenBy,而不是繼續使用OrderBy。

此處僅僅簡單的列出幾項,因為如果執行比較複雜的查詢與投射,將會產生比較繁瑣難懂的代碼。因此,C# 3.0中引入了標準查詢運算式,一種更類似於SQL語言的

二、標準查詢運算式

1、簡單樣本,下段程式碼完成的功能是檢索出不含有*的單詞:

 1 class Program 2     { 3         static string[] Keywords = { "*a", "*b", "*c", "*d", "*e", "*f", "a", "b", "c", "d", "e", "f", "g", "h", "i"}; 4         static void Main(string[] args) 5         { 6             ShowContextualKeyword1(); 7         } 8         public static void ShowContextualKeyword1() 9         {10             IEnumerable<string> selection = from word in Keywords11                                             where !word.Contains(‘*‘)12                                             select word;13             foreach (string s in selection)14             {15                 Console.WriteLine(" " + s);16             }17         }18     }

值得詳細說一下的是類型推斷:select投射回的是word的集合,word的類型是from後面的那個word,從Keywords推斷得到。Keywords是一個string的集合,所以word是string類型,因此select投射到的是IEnumerable<string>

 2、改變傳回型別。

select不僅可以返回原始類型,也可以返回指定的類型,我個人總結的是 他會返回select後面的變數的集合類型。

如下代碼,返回的不是fileName的集合,而是FileInfo的集合:

1   public static void List1(string rootDirectory, string searchPattern)2         {3             IEnumerable<FileInfo> files = from fileName in Directory.GetFiles(rootDirectory, searchPattern)4                                           select new FileInfo(fileName);5             foreach (FileInfo file in files)6             {7                 Console.WriteLine(".{0}({1})",file.Name,file.LastWriteTime);8             }9         }

 

C# 標準查詢運算式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.