LINQ語法
有兩種形式語法,兩著性能上沒區別:
舉例:
假設已定義了一個數組numbers
方法語法:
var A=numbers.Where(x=>x<20);
//這裡用到了Lambda
查詢語法:
var A=from n in numbers
where n<20
select n;
//這裡返回了一個對象,這個對象能形容具體的查詢步驟,這裡並沒有進行查詢動作
查詢變量:
查詢結果一般為一組可枚舉的數據或者一個叫做標量的單一值
一組可枚舉數據:
上面的查詢語法的A不包含查詢結果,相反,編譯器會創建能夠執行這個查詢的代碼
標量:
int count=(from n in numbers where n<20 select n).Count();
//這裡是查詢語句與方法語句的結合
count包含的是真實的整數值,它只能通過真實運行查詢才能得到
查詢表達式結構
注意事項:
from子句
from指定了查詢的數據集合,每一個LINQ查詢都要以from開始
語法:
frome type Item in Items
//type 是可選的,因為編譯器會幫你推斷
與foreach的區別
foreach指定從第一個開始到結束,from之規定集合的每一項都要訪問
foreach遇到代碼就執行主體,from 只是創建可執行查詢的後台代碼對象
jion 聯結:
使用聯結來結合多個集合的數據
聯結操作接受兩個集合然後創建一個臨時的對象集合,每一個對象包含原始集合中的所有字段.
語法:
自我理解:
聯結就是把join前的集合與jion後的集合合成一個集合,當然要設置一些選擇條件
還是看代碼說話
書上:
代碼示例:
1 //這個代碼聲明了兩個類
2 //第一個代表學生,每個學生都有學生的名字和ID
3 //第二個代表課程,每個課程類都有學生的ID和課程的名字
4 class Program
5 {
6 public class Student
7 {
8 public int StID;
9 public string LastName;
10 }
11 public class CourseStudent
12 {
13 public string CourseName;
14 public int StID;
15 }
16 //在全局中創建學生類與課程類
17 //課程類與學生類中有一樣的ID
18 static Student[] students =newStudent[]{
19 new Student{StID=1,LastName="Carson"},
20 new Student{StID=2,LastName="Klassen"},
21 new Student{StID=3,LastName="Fleming"},
22 };
23 static CourseStudent[] studentsInCourses =new CourseStuden t[]{
24 new CourseStudent{CourseName="Art",StID=1},
25 new CourseStudent{CourseName="Art",StID=2},
26 new CourseStudent{CourseName="History",StID=1},
27 new CourseStudent{CourseName="History",StID=3},
28 new CourseStudent{CourseName="Physics",StID=3},
29 };
30 static void Main()
31 {
32 var query =from s in students//開始查詢,在一個集合內查詢
33 join c in studentsInCourses on s.StID equals c.StID
34 //而這個集合是,由兩個集合的部分組成,上面的意思是:
35 //定義students的迭代變量a
36 //定義studentsInCourses迭代變量c
37 //聯結這連個集合中的成員,並生成新的一個集合
38 //聯結結果是:把兩個集合成員具有相同StID的拿出來組成一個新的集合
39 where c.CourseName=="History"
40 //找出新集合中CourseName== "History"的成員
41 select s.LastName;
42 //把這些成員的LastName返回
43 foreach(var q in query)
44 Console.WriteLine("Student taking History: {0}", q);
45 Console.ReadKey();
46 }
47 }
from... let.... where子句
from語句
每一個from語句都引入一個新的數據源
1 class Program
2 {
3 static void Main()
4 {
5 var groupA =new[]{3,4,5,6};
6 var groupB =new[]{6,7,8,9};
7 var someInts =from a in groupA//必須的第一個from語句
8 from b in groupB//主句中的from語句
9 select new{ a, b, sum=a+b };//創建一個匿名類型
10 foreach(var a in someInts )
11 Console.WriteLine( a );//很神奇,"a=","b="自動寫了
12 }
13 }
let子句
接受一個表達式式的運算並且把它復制給一個需要在其他運算中使用的標示符
1 class Program
2 {
3 static void Main()
4 {
5 var groupA =new[]{3,4,5,6};
6 var groupB =new[]{6,7,8,9};
7 var someInts =from a in groupA
8 from b in groupB
9 let sum = a + b
10 where sum ==12
11 select new{ a, b, sum };
12 foreach(var a in someInts )
13 Console.WriteLine( a );
14 }
15 }
where子句
where子句根據之後的運算來去除不符合指點條件的項
注意:
只要在from...let...where部分中,查詢表達式可以是任意個
1 static void Main()
2 {
3 var groupA =new[]{3,4,5,6};
4 var groupB =new[]{6,7,8,9};
5 var someInts =from int a in groupA
6 from int b in groupB
7 let sum = a + b
8 where sum >=11
9 where a ==4
10 select new{ a, b, sum };
11 foreach(var a in someInts )
12 Console.WriteLine( a );
13 }
orderby
orderby子句接受一個表達式並根據表達式按順序返回結果項
表達式通常是字段,但不一定是數值型,也可以是字符串
表達式後面能跟(ascending或descending)//可選的,分別用來設置升序或者降序,默認升序
1 classProgram
2 {
3 static void Main()
4 {
5 var students =new[]
6 {
7 new{LName="Jones",FName="Mary",Age=19,Major="History"},
8 new{LName="Smith",FName="Bob",Age=20,Major="CompSci"},
9 new{LName="Fleming",FName="Carol",Age=21,Major="History"}
10 };
11 var query =from student in students
12 orderby student.Age ascending//降序descending
13 select student;
14 foreach(var s in query)
15 {
16 Console.WriteLine("{0}, {1}: {2} - {3}",
17 s.LName, s.FName, s.Age, s.Major);
18 }
19 Console.ReadKey();
20 }
21 }
1 classProgram
2 {
3 static void Main()
4 {
5 var students =new[]
6 {
7 new{LName="Jones",FName="Mary",Age=19,Major="History"},
8 new{LName="Smith",FName="Bob",Age=20,Major="CompSci"},
9 new{LName="Fleming",FName="Carol",Age=21,Major="History"}
10 };
11 var query =from student in students
12 group student by student.Major;
13 //query是返回的是IEnumerable<IGrouping<鍵,值>>,所以下面不能直接打印
14 foreach(var s in query )
15 {
16 Console.WriteLine("{0}", s.Key);//key是分組鍵
17 foreach(var t in s )
18 Console.WriteLine(" {0}, {1}", t.LName, t.FName);
19 }
20 }
21 }
into子句
插敘延續子句可以接受查詢的一部分結果並賦予新的名字,把它用於其它的查詢
1 classProgram
2 {
3 static void Main()
4 {
5 var groupA =new[]{3,4,5,6};
6 var groupB =new[]{4,5,6,7};
7 var someInts =from a in groupA
8 join b in groupB on a equals b
9 into groupAandB
10 from c in groupAandB
11 //選中groupA與groupB相同的部分把這部分取名為grupAandB
12 select c;
13 foreach(var a in someInts )
14 Console.Write("{0} ", a );
15 }
16 }
Linq核心程序集