LINQ簡介

一、爲什麼要使用LINQ

要理解爲什麼使用LINQ,先來看下面一個例子。假設有一個整數類型的數組,找到裏面的偶數並進行降序排序。

在C#2.0以前,如果要實現這樣的功能,我們必須使用'foreach'或'for'循環來遍歷數組,先找到偶數然後在降序排序,相關代碼如下:

複製代碼

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace LinqOfSelectOperation
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             // 查詢出數組中的偶數並排序
14             int[] ints = { 5, 2, 0, 66, 4, 32, 7, 1 };
15             // 定義一個整數類型的集合,用來存放數組中的偶數
16             List<int> list = new List<int>();
17             // 遍歷數組查詢出偶數放到集合中
18             foreach (int i in ints)
19             {
20                 // 如果是偶數,把偶數加入到集合中
21                 if (i % 2 == 0)
22                 {
23                     list.Add(i);
24                 }
25             }
26 
27             // 正序排序
28             list.Sort();
29             // 反轉
30             list.Reverse();
31             // 輸出
32             Console.WriteLine(string.Join(",",list));
33 
34             Console.ReadKey();
35         }
36     }
37 }

複製代碼

 使用for循環很麻煩,而且不可維護和可讀。C#2.0引入了delegate,可以使用委託來處理這種場景,代碼如下圖所示:

複製代碼

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace LinqOfSelectOperation
 8 {
 9     // 定義委託
10     delegate bool FindEven(int item);
11 
12     class IntExtension
13     {
14         public static int[] where(int[] array, FindEven dele)
15         {
16             int[] result=new int[5];
17             int i = 0;
18             foreach (int item in array)
19             {
20                 if (dele(item))
21                 {
22                    result[i]=item;
23                     i++;
24                 }
25             }
26 
27             return result;
28         }
29     }
30     class Program
31     {
32         static void Main(string[] args)
33         {
34             // 查詢出數組中的偶數並排序
35             int[] ints = { 5, 2, 0, 66, 4, 32, 7, 1 };
36 
37             //delegate(int item){return item % 2 == 0;}表示委託的實現
38             List<int> list = IntExtension.where(ints, delegate(int item)
39             {
40                 return item % 2 == 0;
41             }).ToList();
42             // 正序排序
43             list.Sort();
44             // 反轉
45             list.Reverse();
46             // 輸出
47             Console.WriteLine(string.Join(",", list));
48 
49             Console.ReadKey();
50         }
51     }
52 }

複製代碼

 所以,有了C#2.0,通過使用委託有了代理的優勢,不必使用for循環來查詢不同條件的數組。例如你可以使用相同的委託來查找數組中的奇數,並降序排序輸出,代碼如下圖所示:

複製代碼

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace LinqOfSelectOperation
 8 {
 9     // 定義委託
10     delegate bool FindEven(int item);
11 
12     class IntExtension
13     {
14         public static int[] where(int[] array, FindEven dele)
15         {
16             int[] result=new int[3];
17             int i = 0;
18             foreach (int item in array)
19             {
20                 if (dele(item))
21                 {
22                    result[i]=item;
23                     i++;
24                 }
25             }
26 
27             return result;
28         }
29     }
30     class Program
31     {
32         static void Main(string[] args)
33         {
34             // 查詢出數組中的奇數並排序
35             int[] ints = { 5, 2, 0, 66, 4, 32, 7, 1 };
36 
37             //delegate(int item){return item % 2 != 0;}表示委託的實現
38             List<int> list = IntExtension.where(ints, delegate(int item)
39             {
40                 return item % 2 != 0;
41             }).ToList();
42             // 正序排序
43             list.Sort();
44             // 反轉
45             list.Reverse();
46             // 輸出
47             Console.WriteLine(string.Join(",", list));
48 
49             Console.ReadKey();
50         }
51     }
52 }

複製代碼

 雖然使用delegate可以使程序的可讀性增加了,但是C#團隊認爲他們仍然需要使代碼更加緊湊和可讀,所以他們在C#3.0中引入了擴展方法、Lambda表達式、匿名類型等新特性,你可以使用C#3.0的這些新特性,這些新特性的使用LINQ的前提,可以用來查詢不同類型的集合,並返回需要的結果。

下面的示例演示瞭如何使用LINQ和Lambda表達式根據特定條件來查詢數組,示例代碼如下:

複製代碼

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace LinqOfSelectOperation
 8 {  
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             // 查詢出數組中的奇數並排序
14             int[] ints = { 5, 2, 0, 66, 4, 32, 7, 1 };
15 
16             // 使用LINQ和Lambda表達式查詢數組中的偶數
17             int[] intEvens= ints.Where(p => p % 2 == 0).ToArray();
18             // 使用LINQ和Lambda表達式查詢數組中的奇數
19             int[] intOdds = ints.Where(p => p % 2 != 0).ToArray();
20 
21             // 輸出
22             Console.WriteLine("偶數:" + string.Join(",", intEvens));
23             Console.WriteLine("奇數:" + string.Join(",", intOdds));
24 
25             Console.ReadKey();
26         }
27     }
28 }

複製代碼

 

在上面的例子中可以看到,我們在單個語句中使用LINQ和Lambda表達式指定不同的查詢條件,因此,LINQ使代碼更加緊湊和可讀,並且它也可以用於查詢不同的數據源。看到這裏的時候,你可能會問:究竟什麼是LINQ呢?下面將會具體講解什麼是LINQ。

二、什麼是LINQ

長期以來,開發社區形成以下的格局:

1、面向對象與數據訪問兩個領域長期分裂,各自爲政。

2、編程語言中的數據類型與數據庫中的數據類型形成兩套不同的體系,例如:

  C#中字符串用string數據類型表示。

  SQL中字符串用NVarchar/Varchar/Char數據類型表示。

3、SQL編碼體驗落後

  沒有智能感知效果。

  沒有嚴格意義上的強類型和類型檢查。

4、SQL和XML都有各自的查詢語言,而對象沒有自己的查詢語言。

上面描述的問題,都可以使用LINQ解決,那麼究竟什麼是LINQ呢?

LINQ(Language Integrated Query)即語言集成查詢。

LINQ是一組語言特性和API,使得你可以使用統一的方式編寫各種查詢。用於保存和檢索來自不同數據源的數據,從而消除了編程語言和數據庫之間的不匹配,以及爲不同類型的數據源提供單個查詢接口。

LINQ總是使用對象,因此你可以使用相同的查詢語法來查詢和轉換XML、對象集合、SQL數據庫、ADO.NET數據集以及任何其他可用的LINQ提供程序格式的數據。

LINQ主要包含以下三部分:

1、LINQ to Objects      主要負責對象的查詢。

2、LINQ to XML           主要負責XML的查詢。

3、LINQ to ADO.NET   主要負責數據庫的查詢。

  LINQ to SQL

  LINQ to DataSet

  LINQ to Entities

三、LINQ的優勢

1、熟悉的語言:開發人員不必爲每種類型的數據源或數據格式學習新的語言。

2、更少的編碼:相比較傳統的方式,LINQ減少了要編寫的代碼量。

3、可讀性強:LINQ增加了代碼的可讀性,因此其他開發人員可以很輕鬆地理解和維護。

4、標準化的查詢方式:可以使用相同的LINQ語法查詢多個數據源。

5、類型檢查:程序會在編譯的時候提供類型檢查。

6、智能感知提示:LINQ爲通用集合提供智能感知提示。

7、整形數據:LINQ可以檢索不同形狀的數據。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章