[LINQ] LINQ 入門 (1)

  • 9469
  • 0
  • 2011-07-07

[LINQ] LINQ 入門 (1)

 

LINQ 是擴充方法的組合,讓程式能藉由統一的資料存取模型,以相容的模式存取各種不同的資料來源。LINQ 的核心是 System.Collection.Generic 命名空間中的 IEnumerable<T> 型別,其中所定義的方法成員,程式透過這些方法執行資料存取,資料來源包含了資料庫、文字檔、XML 文件、陣列等。

IEnumerable 介面支援了集合物件元件的列舉操作,程式透過 LINQ 存取後的資料,將會儲存於實作 IEnumerable 介面的物件集合,透過迴圈的方式取得 LINQ 查詢的結果。

以下是個簡單的範例,將陣列中 J 開頭的字串篩選出來

   1: using System;
   2: using System.Text;
   3:  
   4: using System.Collections.Generic;
   5: using System.Linq;
   6:  
   7: namespace ConsoleLinqFirst
   8: {
   9:     class Program
  10:     {
  11:         static void Main(string[] args)
  12:         {
  13:             List<string> nameList = new List<string>
  14:             {
  15:                 "John","Joe","Mary","Katherine","Bob"
  16:             };
  17:  
  18:             Console.WriteLine("原始陣列");
  19:             foreach (string strName in nameList)
  20:             {
  21:                 Console.Write(strName + ", ");
  22:             }
  23:             Console.WriteLine();
  24:             Console.WriteLine("J開頭的字串");
  25:             IEnumerable<string> enumName = nameList.Where(name => name.StartsWith("J"));
  26:             foreach (string strName in enumName)
  27:             {
  28:                 Console.Write(strName + ", ");
  29:             }
  30:  
  31:             Console.ReadKey();
  32:         }
  33:     }
  34: }

關於此範例 :

1. 引用 System.Collections.Generic 與 System.LINQ 命令空間,提供 LINQ 所需型別、介面、類別。

2. 宣告 IEnumerable<string> 型別物件變數 enumName,在等號後透過引用 Where 方法,經由 Lambda 運算式的寫法執行 LINQ 資料運算,將 List 物件集合元素中符合開頭為 J 的字串儲存到 IEnumerable<string> 型別物件回傳,並且由 enumName 變數參數取得,透過 for 迴圈一個個取得 IEnumerable 物件內容並顯示在畫面上。

 

關於範例所使用 Where 方法 :

IEnumerable<string> enumName = nameList.Where(name => name.StartsWith("J"));

 

nameList 是 List 物件,引用 Where 方法,回傳符合條件的資料,Where 是 List<T> 類別的成員,是由 IEnumerable<T> 介面定義的擴充方法,只要是 IEnumerable 型別的物件,都可以引用,透過 LINQ 做資料存取。IEnumerable<T> 介面定義了 LINQ 資料存取規格,真正實作是由 Enumerable 類別完成,上述 nameList 物件所引用的 Where 方法,實際上是 Enumerable 類別提供的實作。

LINQ 運算式是由特定運算子建立的運算子句所構成,運算子對應至特定的方法,例如 where 運算式對應 Where 方法。以上述範例,我們可以將 Where 方法的呼叫,改為使用 where 子句的方式達成。

   1: using System;
   2: using System.Text;
   3:  
   4: using System.Collections.Generic;
   5: using System.Linq;
   6:  
   7: namespace ConsoleLinqFirst
   8: {
   9:     class Program
  10:     {
  11:         static void Main(string[] args)
  12:         {
  13:             List<string> nameList = new List<string>
  14:             {
  15:                 "John","Joe","Mary","Katherine","Bob"
  16:             };
  17:  
  18:             Console.WriteLine("原始陣列");
  19:             foreach (string strName in nameList)
  20:             {
  21:                 Console.Write(strName + ", ");
  22:             }
  23:             Console.WriteLine();
  24:             Console.WriteLine("J 開頭的字串");
  25:  
  26:             // Where 方法呼叫,使用 LINQ 運算式
  27:             // IEnumerable<string> enumName = nameList.Where(name => name.StartsWith("J"));
  28:  
  29:             // where 子句
  30:             IEnumerable<string> enumName =
  31:                 from name in nameList
  32:                 where name.StartsWith("J")
  33:                 select name;
  34:  
  35:             foreach (string strName in enumName)
  36:             {
  37:                 Console.Write(strName + ", ");
  38:             }
  39:  
  40:             Console.ReadKey();
  41:         }
  42:     }
  43: }