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 }
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 }
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 }
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 }
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 }
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 }