C#沉澱-Linq的使用

來自專欄編程改變世界

Linq 可以輕鬆的查詢對象集合。Linq代表語言集成查詢,是.NET框架的擴展,支持從資料庫、程序對象的集合以及XML文檔中查詢數據

一個簡單的示例:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CodeForLinq{ class Program { static void Main(string[] args) { //創建一個int數組,作為被查詢的數據源 int[] numbers = { 1, 2, 3, 4, 5, 18 }; //Linq定義查詢,注意,這裡只是「定義」而已 IEnumerable<int> lowNum = from nu in numbers where nu < 10 select nu; //遍歷lowNum,只有使用lowNum的時候,數據才會被查詢出來 //所以,在這裡才被執行了查詢 foreach (int item in lowNum) { Console.WriteLine(item); } Console.ReadKey(); } }}

針對於不同的數據源,需要實現相應的Linq查詢的代碼模塊,這些代碼模塊被稱作Linq提供程序。在C#中,覺的Linq提供程序有Linq to Object/ Linq to XML/ BLinq(Asp.Net)

匿名類

在深入了解Linq之前,需要先了解一下匿名類,因為在使用Linq語句的時候,會大量的使用匿名類

示例:使用匿名類型創建一個學生類

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CodeForLinq{ class Program { static void Main(string[] args) { //創建一個學生類 var student = new { Name = "Jack", Age = 18, Class = "013" }; Console.ReadKey(); } }}

解析:

創建一個匿名類需要用到關鍵字new,然後直接在後面跟{ }來初始化類中成員(屬性),多個成員之間使用逗號分隔;因為沒有指定類型,所有在接收創建的對象時,需要用到關鍵字var,而且必須使用var關鍵字

  • 匿名類型只能和局部變數配合使用,不能用於類成員
  • 由於匿名類沒有名字,所以必須以var關鍵字作為變數類型
  • 不能設置匿名類型對象的屬性,因為匿名類的成員是只讀的

在初始化一個匿名類型對象時,其成員的初始化不僅可以使用賦值操作,還可以使用成員訪問表達式標識符形式

示例:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CodeForLinq{ class Other { public static string Name = "Bob"; } class Program { static void Main(string[] args) { //局部變數,表示班級 string Class = "013"; //創建一個學生類 var student = new { Other.Name, Age = 18, Class }; //訪問學生類中成員 Console.WriteLine("My name is "+student.Name); Console.WriteLine("Im "+student.Age+" years old"); Console.WriteLine("My Class is " + student.Class); Console.ReadKey(); } }}

var student = new { Other.Name, Age = 18, Class };的效果等同於var student = new { Name = Other.Name, Age = 18, Class = Class};

如果再聲明一個具有相同的參數名、相同的推斷類型和相同順序的匿名類型的話,編譯器會重用這個類型直接創建新的實例,而不會創建新的匿名類型

方法語法和查詢語法

查詢語法:看上去和SQL語句很相似,使用查詢表達形式書寫

方法語法:使用標準的方法調用

查詢語法是聲明式的,但未指明如何執行這個查詢;方法語法是命令式的,它指明了方法查詢調用的順序

編譯器會將使用語法表示的查詢翻譯為方法調用的形式,在運行時這兩種方式沒有性能上的差異

先看方法語法與查詢語法的示例:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CodeForLinq{ class Other { public static string Name = "Bob"; } class Program { static void Main(string[] args) { //創建一個int數組,作為被查詢的數據源 int[] numbers = { 1, 2, 3, 4, 5, 18 }; //查詢語法 var _numbers = from nu in numbers where nu < 10 select nu; //方法語法,Where方法的參數使用了Lambda表達式 var _num = numbers.Where(x => x < 10); foreach (int item in _numbers) { Console.WriteLine(item); } foreach (int item in _num) { Console.WriteLine(item); } Console.ReadKey(); } }}

查詢變數

Linq查詢返回的結果可以是一個枚舉,也可以是一個叫做標量的單一值

示例:

//創建一個int數組,作為被查詢的數據源int[] numbers = { 1, 2, 3, 4, 5, 18 };//返回一個IEnumerable結果,它可以枚舉返回的結果IEnumerable<int> _numbers = from nu in numbers where nu < 10 select nu;//通過Count()方法返回查詢結果總數量int _count = (from nu in numbers where nu < 10 select nu).Count();

等號左邊的變數叫做查詢變數,這裡指_numbers_count

查詢變數一般使用var類型來讓編譯器自動推斷其返回的類型

如果查詢語句返回的是枚舉類型,查詢變數中是不會包含結果的,只有在真正使用枚舉值的時候才會執行查詢,並且每次使用枚舉值的時候都會執行一次查詢語句;而如果查詢語句返回的是標題,查詢則立即生效,並把結果保存在查詢變數中

查詢表達式的結構

from子名指定數據源,並且引入迭代變數;迭代變數逐個表示數據源的每一個元素;語法如下:

from [Type] item in ItemsItems表示數據源;item表示數據源中的元素;Type是可選的,表示元素的類型

join子句,聯結語句可以結合兩個或多個集合中的數據,然後產生一個臨時的對象集合,每個集合中都包含原始集合對象中的所有元素,語法如下:

join Identifier in Collection2 on Field1 equqls Field2

示例:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CodeForLinq{ class Course//課程類 { public int ID; public int Student_ID; public string Course_Name; } class Student//學生類 { public int ID; public string Name; } class Program { static void Main(string[] args) { //學生類集合 Student[] st = new Student[] { new Student{ID=111,Name="Bob"}, new Student{ID=112,Name="Jack"}, new Student{ID=113,Name="Hong"} }; //課程類集合 Course[] co = new Course[] { new Course{ID=1, Student_ID=111,Course_Name="數學"}, new Course{ID=2, Student_ID=112,Course_Name="語文"}, new Course{ID=3, Student_ID=113,Course_Name="化學"}, new Course{ID=4, Student_ID=112,Course_Name="數學"}, new Course{ID=5, Student_ID=112,Course_Name="生物"} }; //Linq查詢語法 var result = from a in st //指定第一個數據源st join b in co on a.ID equals b.Student_ID //聯結第二個數據源ot,並用on指定聯結條件,equals來指定比較欄位 where b.Course_Name=="數學" //匹配數學課程 select a.Name; //返回名字 foreach (var name in result) { Console.WriteLine("參加數學課程的學生名:"+name); } Console.ReadKey(); } }}

from...let...where片段

可以使用多個from子句指定多個數據源,示例:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CodeForLinq{ class Course//課程類 { public int ID; public int Student_ID; public string Course_Name; } class Student//學生類 { public int ID; public string Name; } class Program { static void Main(string[] args) { //學生類集合 Student[] st = new Student[] { new Student{ID=111,Name="Bob"}, new Student{ID=112,Name="Jack"}, new Student{ID=113,Name="Hong"} }; //課程類集合 Course[] co = new Course[] { new Course{ID=1, Student_ID=111,Course_Name="數學"}, new Course{ID=2, Student_ID=112,Course_Name="語文"}, new Course{ID=3, Student_ID=113,Course_Name="化學"}, new Course{ID=4, Student_ID=112,Course_Name="數學"}, new Course{ID=5, Student_ID=112,Course_Name="生物"} }; //指定多個數據源 var st_co = from a in st from b in co where a.ID == b.Student_ID && b.Course_Name=="數學" select new { a.Name, b.Course_Name };//創建一個匿名類型對象 //訪問返回集體中的成員 foreach (var item in st_co) { Console.WriteLine("學生:"+item.Name); Console.WriteLine("課程:"+item.Course_Name); } Console.ReadKey(); } }}

let子句接受一個表達式的運算,並且把它賦值給一個需要在其它地方運算中使用的標識符

示例:

//定義兩個數據源int[] number1 = { 1, 2, 3, 4, 5 };int[] numbers2 = { 1, 2, 3, 4, 5, 18 };var nu_array = from a in number1 from b in numbers2 let sum = a + b //使用let子句將第一個集合中的元素與第二個集合中的元素進行相加 where sum == 4 select new { a, b, sum };foreach (var item in nu_array){ Console.WriteLine(item.a + "," + item.b + "," + item.sum);}

where子句根據之後運算來去除不符合指定條件的項,在from...let...where片段中可以有任意多個where子句

示例:

//定義兩個數據源int[] number1 = { 1, 2, 3, 4, 5 };int[] numbers2 = { 1, 2, 3, 4, 5, 18 };var nu_array = from a in number1 from b in numbers2 let sum = a + b //使用let子句將第一個集合中的元素與第二個集合中的元素進行相加 where sum == 4 //篩選a+b等於4的所有元素 where a == 2 //再指定a必須等於2,那返回的結果中,b就只能是等於2了 select new { a, b, sum };foreach (var item in nu_array){ Console.WriteLine(item.a + "," + item.b + "," + item.sum);}

orderby子句

orderby子句接受一個表達式,並根據表達式按順序返回結果;排列的表達式也可以是集合中的成員

  • orderby子句默認是按升序排列的;可以使用ascending顯示的指定為升序或使用descending指定為隆序
  • 可以有任意多個子句,之間使用逗號分隔

示例:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CodeForLinq{ class Course//課程類 { public int ID; public int Student_ID; public string Course_Name; } class Student//學生類 { public int ID; public string Name; public int Age; } class Program { static void Main(string[] args) { //學生類集合 Student[] st = new Student[] { new Student{ID=111,Name="Bob",Age=12}, new Student{ID=112,Name="Jack",Age=15}, new Student{ID=113,Name="Hong",Age=9} }; //課程類集合 Course[] co = new Course[] { new Course{ID=1, Student_ID=111,Course_Name="數學"}, new Course{ID=2, Student_ID=112,Course_Name="語文"}, new Course{ID=3, Student_ID=113,Course_Name="化學"}, new Course{ID=4, Student_ID=112,Course_Name="數學"}, new Course{ID=5, Student_ID=112,Course_Name="生物"} }; var query = from student in st orderby student.Age // 根據Age欄位進行排序 select student; foreach (var item in query) { Console.WriteLine(string.Format("ID:{0},名字:{1},年齡:{2}",item.ID,item.Name,item.Age)); } Console.ReadKey(); } }}

select...group子句

select子句 指定所選對象的哪部分應該被選擇;指定的部分可以是整個數據項,或數據項的一個欄位,或數據項的幾個欄位組成的新的對象

group by子句 是可選的,用來指定選擇的項如何分組

select子句示例:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CodeForLinq{ class Student//學生類 { public int ID; public string Name; public int Age; } class Program { static void Main(string[] args) { //學生類集合 Student[] st = new Student[] { new Student{ID=111,Name="Bob",Age=12}, new Student{ID=112,Name="Jack",Age=15}, new Student{ID=113,Name="Hong",Age=9} }; var query = from student in st //select student.Name //選擇一個欄位 //select new {student.Name, student.Age} //選擇多個欄位組成的新對象 select student;// 選擇所有的sutdent元素 foreach (var item in query) { Console.WriteLine(string.Format("ID:{0},名字:{1},年齡:{2}",item.ID,item.Name,item.Age)); } Console.ReadKey(); } }}

查詢中的匿名類——查詢結果可以由原始集合的項、項的某些欄位或匿名類型組成,例如select new {student.Name, student.Age}

group子句將select的對象根據一些標準進行分組

  • 如果項包含在查詢語句中,它就可以根據某個欄位的值進行分組;作為分組的依據的屬性叫做健(key)
  • gorup將返回可以枚舉已經形成的項的分組的可枚舉類型
  • 分組本身是可被枚舉的

示例:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CodeForLinq{ class Student//學生類 { public int ID; public string Name; public int Age; } class Program { static void Main(string[] args) { //學生類集合 Student[] st = new Student[] { new Student{ID=111,Name="Bob",Age=12}, new Student{ID=112,Name="Jack",Age=15}, new Student{ID=113,Name="Json",Age=15}, new Student{ID=114,Name="Hong",Age=9} }; var query = from student in st group student by student.Age; //按照年齡來分組 foreach (var item in query) { Console.WriteLine("年齡組:"+item.Key); //通過Key來找到分組的依據 foreach (var it in item) { Console.WriteLine(" 名字:"+it.Name); } } Console.ReadKey(); } }}

查詢延續:into子句

查詢延續子句可以接受查詢的一部分結果並賦予一個名字,從而可以在查詢的另一部分中使用

示例:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CodeForLinq{ class Course//課程類 { public int ID; public int Student_ID; public string Course_Name; } class Student//學生類 { public int ID; public string Name; public int Age; } class Program { static void Main(string[] args) { int[] number1 = { 1, 2, 3, 4, 5 }; int[] numbers2 = { 1, 2, 3, 4, 5, 18 }; var result = from a in number1 join b in numbers2 on a equals b into a_b //通過into將number1與numbers2聯合命名為a_b from c in a_b select c; foreach (var item in result) { Console.WriteLine(item); } Console.ReadKey(); } }}

標準查詢運算符

  • 被查詢的對象叫做序列,它必須實現IEnumberable介面
  • 標準查詢運算符使用方法語法
  • 一些運算符返回IEnumberable對象,而其他的一些 運算符返回標量
  • 很多操作都可以一個Lambda表達式做為參數

示例:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CodeForLinq{ class Course//課程類 { public int ID; public int Student_ID; public string Course_Name; } class Student//學生類 { public int ID; public string Name; public int Age; } class Program { static void Main(string[] args) { int[] number = { 1, 2, 3, 4, 5 }; //total與hwoMay都是標量 //被操作的對象number是一個序列 //而Sum()與Count()是去處符(方法) int total = number.Sum(); int howMany = number.Count(); Console.ReadKey(); } }}

序列是指實現了IEnumberable介面的類,包括List<>/Dictionary<>/Stack<>/Array等待

共有47個標題運算符,下面列舉幾個常用的運算符:

  1. Where -- 根據給定的條件對序列進行過濾
  2. Select -- 指定要包含一個對象或
  3. Join -- 對兩個序列執行內聯結
  4. GroupBy -- 分組序列中的元素
  5. Dinstinct -- 去除序列中的重複項
  6. ToList -- 將序列作為List返回
  7. First -- 返回序列中第一個與條件相匹配的元素
  8. FirstOrDefault -- 返回序列中第一個與條件相匹配的元素,如果匹配不到,就返回第一個元素
  9. Last -- 返回序列中最後一個與條件相匹配的元素
  10. LastOrDefault -- 返回序列中最後一個與條件相匹配的元素,如果匹配不到,就返回最後一個元素
  11. Count -- 返回序列中元素的個數
  12. Sum -- 返回序列中值的總和
  13. Min -- 返回序列中值的最小值
  14. Max -- 返回序列中值的最大值
  15. Average -- 返回序列中值的平均值
  16. Contains -- 返回一個布爾值,指明序列中是否包含某個元素

System.Linq.Enumberable類聲明了標準查詢運算符方法,它們都擴展了IEnumberable泛型類的擴展方法

  • 由於運算符是泛型方法, 因此每個方法名都具有相關泛型參數(T)
  • 由於運算符是擴展IEnumberable的擴展方法,它們必須滿足以下的語法條件
  • 聲明為Public和Static
  • 在第一個參數前有this指示器
  • IEnumberable作為第一個參數類型

示例:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CodeForLinq{ class Program { static void Main(string[] args) { int[] number = new int[] { 1, 2, 3, 4, 5 }; //方法語法,數組作為參數 var _count = Enumerable.Count(number); var _first = Enumerable.First(number); //擴展語法,數組被做為被擴展的對象 var __count = number.Count(); var __first = number.First(); Console.ReadKey(); } }}

每一個查詢表達式都會被編譯器翻譯成標準查詢運算符的形式,兩者可以結合使用,示例:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CodeForLinq{ class Program { static void Main(string[] args) { int[] number = new int[] { 1, 2, 3, 4, 5 }; int _num = (from nu in number where nu < 4 select nu).Count(); Console.ReadKey(); } }}

推薦閱讀:

相关文章